J
jeff M via .NET 247
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I get the correct answer by taking the decimal valueand using that as an index to an array that will map to thecorrect EBCDIC value in hex.
By larger values, an example would be "AA" in EBCDIC hex wouldgive me the value of 63 in decimal (ASCII) when read from thefile. My table would then map to "6F". I'm still not sure whatis going wrong, so any help would be great.
Someone did post some help regarding a Microsoft page but I wasunable to link it back to VB.net through the name spaces. Thehelp was in VB6 and VB5.
Private Sub OpenAndProcessFile(ByVal sInputFileName AsString)
Dim FileStream As System.IO.FileStream = NewSystem.IO.FileStream(sInputFileName, _
IO.FileMode.Open,IO.FileAccess.Read, IO.FileShare.Read)
'Dim StreamIn As System.IO.StreamReader = NewSystem.IO.StreamReader(FileStream,System.Text.Encoding.GetEncoding(1252))
Dim StreamIn As System.IO.StreamReader = NewSystem.IO.StreamReader(FileStream,System.Text.Encoding.GetEncoding("ASCII"))
Dim tmp As Byte
Dim bHigh, bLow As Byte
Static first_time_through As Boolean = True
Dim count As Int16 = 0
While (StreamIn.Peek > -1) And (count <> -1)
count += 1
MsgBox(StreamIn.Peek)
tmp = ASCII_TO_EBCDIC(StreamIn.Read)
If (count = 1) And (tmp = &HC2) Then
While (count < 2048) And (StreamIn.Peek > -1)
tmp = StreamIn.Read
count += 1
End While
End If
Extract_Bits(tmp, bHigh, bLow, first_time_through)
If count = 10 Then
MTXDisplay.AppendText(Chr(13) + Chr(10))
ElseIf ((count - 10) Mod 252) = 0 Then
MTXDisplay.AppendText(Chr(13) + Chr(10))
ElseIf (count > 2049) Then
MTXDisplay.AppendText(Chr(13) + Chr(10))
count = 0
If (MsgBox("Continue", MsgBoxStyle.OKCancel) =MsgBoxResult.Cancel) Then
'check to continue at the end of each 2Kblock
count = -1
End If
End If
End While
StreamIn.Close()
FileStream.Close()
'Close()
End Sub
Private Sub Extract_Bits(ByVal ByteIn As Byte, ByRef bHigh AsByte, ByRef bLow As Byte, _
ByRef first_time_through AsBoolean)
'ByteIn = ASCII_TO_EBCDIC(ByteIn)
bHigh = (ByteIn And &HF0) \ 16
bLow = (ByteIn And &HF)
If (first_time_through) Then
MTXDisplay.Text = Hex(bHigh) + Hex(bLow)
first_time_through = False
Else
MTXDisplay.AppendText(Hex(bHigh) + Hex(bLow))
End If
End Sub
Private Function ASCII_TO_EBCDIC(ByVal ByteIn As Byte) AsByte
Dim EBCDIC_Table() As Byte = {&H0, &H1, &H2, &H3, &H37,&H2D, &H2E, &H2F, &H16, &H5, &H15, _
&HB, &HC, &HD, &HE, &HF,&H10, &H11, &H12, &H13, &H3C, &H3D, _
&H32, &H26, &H18, &H19,&H3F, &H27, &H1C, &H1D, &H1E, &H1F, &H40, _
&H5A, &H7F, &H7B, &H5B,&H6C, &H50, &H7D, &H4D, &H5D, &H5C, &H4E, _
&H6B, &H60, &H4B, &H61,&HF0, &HF1, &HF2, &HF3, &HF4, &HF5, &HF6, _
&HF7, &HF8, &HF9, &H7A,&H5E, &H4C, &H7E, &H6E, &H6F, &H7C, &HC1, _
&HC2, &HC3, &HC4, &HC5,&HC6, &HC7, &HC8, &HC9, &HD1, &HD2, &HD3, _
&HD4, &HD5, &HD6, &HD7,&HD8, &HD9, &HE2, &HE3, &HE4, &HE5, &HE6, _
&HE7, &HE8, &HE9, &H8D,&HE0, &H9D, &H5F, &H6D, &H79, &H81, &H82, _
&H83, &H84, &H85, &H86,&H87, &H88, &H89, &H91, &H92, &H93, &H94, _
&H95, &H96, &H97, &H98,&H99, &HA2, &HA3, &HA4, &HA5, &HA6, &HA7, _
&HA8, &HA9, &HC0, &H4F,&HD0, &HA1, &H7, &H20, &H21, &H22, &H23, _
&H24, &H15, &H6, &H17,&H28, &H29, &H2A, &H2B, &H2C, &H9, &HA, _
&H1B, &H30, &H31, &H33,&H34, &H35, &H36, &H8, &H38, &H39, &H3A, _
&H3B, &H4, &H14, &H3E,&HE1, &H20, &H41, &H42, &H43, &H44, &H45, _
&H46, &H47, &H48, &H49,&H51, &H52, &H53, &H54, &H55, &H56, &H57, _
&H58, &H59, &H62, &H63,&H64, &H65, &H66, &H67, &H68, &H69, &H70, _
&H71, &H72, &H73, &H74,&H75, &H76, &H77, &H78, &H80, &H8A, &H8B, _
&H8C, &H8D, &H8E, &H8F,&H90, &H9A, &H9B, &H9C, &H9D, &H9E, &H9F, _
&HA0, &HAA, &HAB, &HAC,&HAD, &HAE, &HAF, &HB0, &HB1, &HB2, &HB3, _
&HB4, &HB5, &HB6, &HB7,&HB8, &HB9, &HBA, &HBB, &HBC, &HBD, &HBE, _
&HBF, &HCA, &HCB, &HCC,&HCD, &HCE, &HCF, &HDA, &HDB, &HDC, &HDD, _
&HDE, &HDF, &HEA, &HEB,&HEC, &HED, &HEE, &HEF, &HFA, &HFB, &HFC, _
&HFD, &HFE, &HFF}
ASCII_TO_EBCDIC = EBCDIC_Table(ByteIn)
End Function
By larger values, an example would be "AA" in EBCDIC hex wouldgive me the value of 63 in decimal (ASCII) when read from thefile. My table would then map to "6F". I'm still not sure whatis going wrong, so any help would be great.
Someone did post some help regarding a Microsoft page but I wasunable to link it back to VB.net through the name spaces. Thehelp was in VB6 and VB5.
Private Sub OpenAndProcessFile(ByVal sInputFileName AsString)
Dim FileStream As System.IO.FileStream = NewSystem.IO.FileStream(sInputFileName, _
IO.FileMode.Open,IO.FileAccess.Read, IO.FileShare.Read)
'Dim StreamIn As System.IO.StreamReader = NewSystem.IO.StreamReader(FileStream,System.Text.Encoding.GetEncoding(1252))
Dim StreamIn As System.IO.StreamReader = NewSystem.IO.StreamReader(FileStream,System.Text.Encoding.GetEncoding("ASCII"))
Dim tmp As Byte
Dim bHigh, bLow As Byte
Static first_time_through As Boolean = True
Dim count As Int16 = 0
While (StreamIn.Peek > -1) And (count <> -1)
count += 1
MsgBox(StreamIn.Peek)
tmp = ASCII_TO_EBCDIC(StreamIn.Read)
If (count = 1) And (tmp = &HC2) Then
While (count < 2048) And (StreamIn.Peek > -1)
tmp = StreamIn.Read
count += 1
End While
End If
Extract_Bits(tmp, bHigh, bLow, first_time_through)
If count = 10 Then
MTXDisplay.AppendText(Chr(13) + Chr(10))
ElseIf ((count - 10) Mod 252) = 0 Then
MTXDisplay.AppendText(Chr(13) + Chr(10))
ElseIf (count > 2049) Then
MTXDisplay.AppendText(Chr(13) + Chr(10))
count = 0
If (MsgBox("Continue", MsgBoxStyle.OKCancel) =MsgBoxResult.Cancel) Then
'check to continue at the end of each 2Kblock
count = -1
End If
End If
End While
StreamIn.Close()
FileStream.Close()
'Close()
End Sub
Private Sub Extract_Bits(ByVal ByteIn As Byte, ByRef bHigh AsByte, ByRef bLow As Byte, _
ByRef first_time_through AsBoolean)
'ByteIn = ASCII_TO_EBCDIC(ByteIn)
bHigh = (ByteIn And &HF0) \ 16
bLow = (ByteIn And &HF)
If (first_time_through) Then
MTXDisplay.Text = Hex(bHigh) + Hex(bLow)
first_time_through = False
Else
MTXDisplay.AppendText(Hex(bHigh) + Hex(bLow))
End If
End Sub
Private Function ASCII_TO_EBCDIC(ByVal ByteIn As Byte) AsByte
Dim EBCDIC_Table() As Byte = {&H0, &H1, &H2, &H3, &H37,&H2D, &H2E, &H2F, &H16, &H5, &H15, _
&HB, &HC, &HD, &HE, &HF,&H10, &H11, &H12, &H13, &H3C, &H3D, _
&H32, &H26, &H18, &H19,&H3F, &H27, &H1C, &H1D, &H1E, &H1F, &H40, _
&H5A, &H7F, &H7B, &H5B,&H6C, &H50, &H7D, &H4D, &H5D, &H5C, &H4E, _
&H6B, &H60, &H4B, &H61,&HF0, &HF1, &HF2, &HF3, &HF4, &HF5, &HF6, _
&HF7, &HF8, &HF9, &H7A,&H5E, &H4C, &H7E, &H6E, &H6F, &H7C, &HC1, _
&HC2, &HC3, &HC4, &HC5,&HC6, &HC7, &HC8, &HC9, &HD1, &HD2, &HD3, _
&HD4, &HD5, &HD6, &HD7,&HD8, &HD9, &HE2, &HE3, &HE4, &HE5, &HE6, _
&HE7, &HE8, &HE9, &H8D,&HE0, &H9D, &H5F, &H6D, &H79, &H81, &H82, _
&H83, &H84, &H85, &H86,&H87, &H88, &H89, &H91, &H92, &H93, &H94, _
&H95, &H96, &H97, &H98,&H99, &HA2, &HA3, &HA4, &HA5, &HA6, &HA7, _
&HA8, &HA9, &HC0, &H4F,&HD0, &HA1, &H7, &H20, &H21, &H22, &H23, _
&H24, &H15, &H6, &H17,&H28, &H29, &H2A, &H2B, &H2C, &H9, &HA, _
&H1B, &H30, &H31, &H33,&H34, &H35, &H36, &H8, &H38, &H39, &H3A, _
&H3B, &H4, &H14, &H3E,&HE1, &H20, &H41, &H42, &H43, &H44, &H45, _
&H46, &H47, &H48, &H49,&H51, &H52, &H53, &H54, &H55, &H56, &H57, _
&H58, &H59, &H62, &H63,&H64, &H65, &H66, &H67, &H68, &H69, &H70, _
&H71, &H72, &H73, &H74,&H75, &H76, &H77, &H78, &H80, &H8A, &H8B, _
&H8C, &H8D, &H8E, &H8F,&H90, &H9A, &H9B, &H9C, &H9D, &H9E, &H9F, _
&HA0, &HAA, &HAB, &HAC,&HAD, &HAE, &HAF, &HB0, &HB1, &HB2, &HB3, _
&HB4, &HB5, &HB6, &HB7,&HB8, &HB9, &HBA, &HBB, &HBC, &HBD, &HBE, _
&HBF, &HCA, &HCB, &HCC,&HCD, &HCE, &HCF, &HDA, &HDB, &HDC, &HDD, _
&HDE, &HDF, &HEA, &HEB,&HEC, &HED, &HEE, &HEF, &HFA, &HFB, &HFC, _
&HFD, &HFE, &HFF}
ASCII_TO_EBCDIC = EBCDIC_Table(ByteIn)
End Function