473,562 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading EBCDIC Files.

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 OpenAndProcessF ile(ByVal sInputFileName AsString)
Dim FileStream As System.IO.FileS tream = NewSystem.IO.Fi leStream(sInput FileName, _
IO.FileMode.Ope n,IO.FileAccess .Read, IO.FileShare.Re ad)
'Dim StreamIn As System.IO.Strea mReader = NewSystem.IO.St reamReader(File Stream,System.T ext.Encoding.Ge tEncoding(1252) )
Dim StreamIn As System.IO.Strea mReader = NewSystem.IO.St reamReader(File Stream,System.T ext.Encoding.Ge tEncoding("ASCI I"))
Dim tmp As Byte
Dim bHigh, bLow As Byte
Static first_time_thro ugh 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(tm p, bHigh, bLow, first_time_thro ugh)
If count = 10 Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
ElseIf ((count - 10) Mod 252) = 0 Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
ElseIf (count > 2049) Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
count = 0
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =MsgBoxResult.C ancel) Then
'check to continue at the end of each 2Kblock
count = -1
End If
End If
End While
StreamIn.Close( )
FileStream.Clos e()
'Close()
End Sub

Private Sub Extract_Bits(By Val ByteIn As Byte, ByRef bHigh AsByte, ByRef bLow As Byte, _
ByRef first_time_thro ugh AsBoolean)
'ByteIn = ASCII_TO_EBCDIC (ByteIn)
bHigh = (ByteIn And &HF0) \ 16
bLow = (ByteIn And &HF)
If (first_time_thr ough) Then
MTXDisplay.Text = Hex(bHigh) + Hex(bLow)
first_time_thro ugh = False
Else
MTXDisplay.Appe ndText(Hex(bHig h) + 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(By teIn)
End Function
--------------------------------
From: jeff M

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tLPJdMVh30K +5IxRhsrpUA==</Id>
Nov 20 '05 #1
9 6741
Hi, jeff

I think you have to consider that you need different tables for different
data types. For example, packed is not same as signed and not same as text.
Text is language dependent. When I did similar exercise I first detected
which data type must be decoded and only then used corresponding table.
That's why you can more or less decode only lower range, which is more
stable than higher one.

HTH
Alex

"jeff M via .NET 247" <an*******@dotn et247.com> wrote in message
news:el******** ******@tk2msftn gp13.phx.gbl...
I'm still having problems reading EBCDIC files. Currently it looks like the
lower range (0 to 127) is working. I have tried the following code pages
20284, 20924, 1140, 37, 500 and 20127. By working I get the correct answer
by taking the decimal value and using that as an index to an array that will
map to the correct EBCDIC value in hex.

By larger values, an example would be "AA" in EBCDIC hex would give me the
value of 63 in decimal (ASCII) when read from the file. My table would then
map to "6F". I'm still not sure what is going wrong, so any help would be
great.

Someone did post some help regarding a Microsoft page but I was unable to
link it back to VB.net through the name spaces. The help was in VB6 and VB5.
Private Sub OpenAndProcessF ile(ByVal sInputFileName As String)
Dim FileStream As System.IO.FileS tream = New
System.IO.FileS tream(sInputFil eName, _
IO.FileMode.Ope n,
IO.FileAccess.R ead, IO.FileShare.Re ad)
'Dim StreamIn As System.IO.Strea mReader = New
System.IO.Strea mReader(FileStr eam, System.Text.Enc oding.GetEncodi ng(1252))
Dim StreamIn As System.IO.Strea mReader = New
System.IO.Strea mReader(FileStr eam,
System.Text.Enc oding.GetEncodi ng("ASCII"))
Dim tmp As Byte
Dim bHigh, bLow As Byte
Static first_time_thro ugh 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(tm p, bHigh, bLow, first_time_thro ugh)
If count = 10 Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
ElseIf ((count - 10) Mod 252) = 0 Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
ElseIf (count > 2049) Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
count = 0
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =
MsgBoxResult.Ca ncel) Then
'check to continue at the end of each 2K block
count = -1
End If
End If
End While
StreamIn.Close( )
FileStream.Clos e()
'Close()
End Sub

Private Sub Extract_Bits(By Val ByteIn As Byte, ByRef bHigh As Byte,
ByRef bLow As Byte, _
ByRef first_time_thro ugh As Boolean)
'ByteIn = ASCII_TO_EBCDIC (ByteIn)
bHigh = (ByteIn And &HF0) \ 16
bLow = (ByteIn And &HF)
If (first_time_thr ough) Then
MTXDisplay.Text = Hex(bHigh) + Hex(bLow)
first_time_thro ugh = False
Else
MTXDisplay.Appe ndText(Hex(bHig h) + Hex(bLow))
End If
End Sub

Private Function ASCII_TO_EBCDIC (ByVal ByteIn As Byte) As Byte
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(By teIn)
End Function
--------------------------------
From: jeff M

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tLPJdMVh30K +5IxRhsrpUA==</Id>
Nov 20 '05 #2
Jeff,
Is the EBCDIC file straight text or does it have packed or binary fields in
it? (you mentioned BCD in your other post which leads me to believe Packed
or binary Fields).

If you have straight text, the System.Text.Enc oding class should do all the
work for you. If possible I would format the file as straight text on the
Mainframe.

If you have packed fields, I would recommend using a BinaryReader & not a
StreamReader. The BinaryReader will allow you to read the packed fields as
is, while you can then use a EBCDIC (code page 37 & 500)
System.Text.Enc oding object to convert the text fields an array of EBCDIC
bytes to a String.

Remember that a String in .NET is always in Unicode. When you read or write
that string externally you can use System.Text.Enc oding to translate it to &
from a specific encoding, such as EBCDIC. However with packed or binary
fields, you do not want automatic translation to take place, you only want
to translate

Following is a program that will convert an EBCDIC file to a ANSI file.

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Text

Public Class EBCDICSample

Private Const EBCDICCodePage As Integer = 37 ' 37, 500

Public Shared Sub Main()
CopyToEBCDIC("E BCDICSample.vb" , "EBCDICSample.e bcdic")
CopyToAnsi("EBC DICSample.ebcdi c", "EBCDICSample.a nsi")
End Sub

Private Shared Sub CopyToAnsi(ByVa l inputPath As String, ByVal
outputPath As String)
Dim EBCDIC As Encoding = Encoding.GetEnc oding(EBCDICCod ePage)
Dim input As New StreamReader(in putPath, EBCDIC)
Dim output As New StreamWriter(ou tputPath, False, Encoding.Defaul t)

Dim line As String

line = input.ReadLine( )
Do Until line Is Nothing
output.WriteLin e(line)
line = input.ReadLine( )
Loop
input.Close()
output.Close()
End Sub

Private Shared Sub CopyToEBCDIC(By Val inputPath As String, ByVal
outputPath As String)
Dim EBCDIC As Encoding = Encoding.GetEnc oding(EBCDICCod ePage)
Dim input As New StreamReader(in putPath, Encoding.Defaul t)
Dim output As New StreamWriter(ou tputPath, False, EBCDIC)

Dim line As String

line = input.ReadLine( )
Do Until line Is Nothing
output.WriteLin e(line)
line = input.ReadLine( )
Loop
input.Close()
output.Close()
End Sub

End Class

Hope this helps
Jay
"jeff M via .NET 247" <an*******@dotn et247.com> wrote in message
news:el******** ******@tk2msftn gp13.phx.gbl...
I'm still having problems reading EBCDIC files. Currently it looks like the
lower range (0 to 127) is working. I have tried the following code pages
20284, 20924, 1140, 37, 500 and 20127. By working I get the correct answer
by taking the decimal value and using that as an index to an array that will
map to the correct EBCDIC value in hex.

By larger values, an example would be "AA" in EBCDIC hex would give me the
value of 63 in decimal (ASCII) when read from the file. My table would then
map to "6F". I'm still not sure what is going wrong, so any help would be
great.

Someone did post some help regarding a Microsoft page but I was unable to
link it back to VB.net through the name spaces. The help was in VB6 and VB5.

<<snip>>
Nov 20 '05 #3
Jeff,
In addition to my other comments:
Currently it looks like the lower range (0 to 127) Dim StreamIn As System.IO.Strea mReader = _
New System.IO.Strea mReader(FileStr eam, _
System.Text.Enc oding.GetEncodi ng("ASCII"))


Remember that ASCII has code points from 0 to 127. You could use ANSI, which
is 0 to 255, however you indicated that the file itself is EBCDIC, so you
should use an EBCDIC encoding (37 or 500) & the framework will translate the
EBCDIC text to Unicode for you. StreamIn.Read will always return an Unicode
character (-1 for end of file). StreamIn.Read will use the encoding object
to convert the code point in the file to this Unicode character.

Hope this helps
Jay

"jeff M via .NET 247" <an*******@dotn et247.com> wrote in message
news:el******** ******@tk2msftn gp13.phx.gbl...
I'm still having problems reading EBCDIC files. Currently it looks like the
lower range (0 to 127) is working. I have tried the following code pages
20284, 20924, 1140, 37, 500 and 20127. By working I get the correct answer
by taking the decimal value and using that as an index to an array that will
map to the correct EBCDIC value in hex.

By larger values, an example would be "AA" in EBCDIC hex would give me the
value of 63 in decimal (ASCII) when read from the file. My table would then
map to "6F". I'm still not sure what is going wrong, so any help would be
great.

Someone did post some help regarding a Microsoft page but I was unable to
link it back to VB.net through the name spaces. The help was in VB6 and VB5.
Private Sub OpenAndProcessF ile(ByVal sInputFileName As String)
Dim FileStream As System.IO.FileS tream = New
System.IO.FileS tream(sInputFil eName, _
IO.FileMode.Ope n,
IO.FileAccess.R ead, IO.FileShare.Re ad)
'Dim StreamIn As System.IO.Strea mReader = New
System.IO.Strea mReader(FileStr eam, System.Text.Enc oding.GetEncodi ng(1252))
Dim StreamIn As System.IO.Strea mReader = New
System.IO.Strea mReader(FileStr eam,
System.Text.Enc oding.GetEncodi ng("ASCII"))
Dim tmp As Byte
Dim bHigh, bLow As Byte
Static first_time_thro ugh 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(tm p, bHigh, bLow, first_time_thro ugh)
If count = 10 Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
ElseIf ((count - 10) Mod 252) = 0 Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
ElseIf (count > 2049) Then
MTXDisplay.Appe ndText(Chr(13) + Chr(10))
count = 0
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =
MsgBoxResult.Ca ncel) Then
'check to continue at the end of each 2K block
count = -1
End If
End If
End While
StreamIn.Close( )
FileStream.Clos e()
'Close()
End Sub

Private Sub Extract_Bits(By Val ByteIn As Byte, ByRef bHigh As Byte,
ByRef bLow As Byte, _
ByRef first_time_thro ugh As Boolean)
'ByteIn = ASCII_TO_EBCDIC (ByteIn)
bHigh = (ByteIn And &HF0) \ 16
bLow = (ByteIn And &HF)
If (first_time_thr ough) Then
MTXDisplay.Text = Hex(bHigh) + Hex(bLow)
first_time_thro ugh = False
Else
MTXDisplay.Appe ndText(Hex(bHig h) + Hex(bLow))
End If
End Sub

Private Function ASCII_TO_EBCDIC (ByVal ByteIn As Byte) As Byte
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(By teIn)
End Function
--------------------------------
From: jeff M

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tLPJdMVh30K +5IxRhsrpUA==</Id>
Nov 20 '05 #4
My file contains Binary Coded Decimal only. So with that in mind I
started to look into the BinaryRead. I must admit, I'm getting very
close now but it's still not 100%. I tried using just StreamIn.Read
and assign the value to a Byte field. The problem was I would get the
odd overflow error so I started to convert the byte in to an integer
using ASC.

Thoughts?

Thanks for the help, my hairline appreciates it.
Jeff

Private Sub OpenAndProcessF ile(ByVal sInputFileName As String)
Dim FileStream As System.IO.FileS tream = New
System.IO.FileS tream(sInputFil eName, _
IO.FileMode.Ope n, IO.FileAccess.R ead, IO.FileShare.Re ad)
Dim StreamIn As BinaryReader = New BinaryReader(Fi leStream)
Dim FileOutStream As FileStream = New
FileStream("tmp _out.txt", FileMode.OpenOr Create)
Dim StreamOut As StreamWriter = New
StreamWriter(Fi leOutStream)
Dim tmp As Int16
Dim bHigh, bLow As Byte
Static first_time_thro ugh As Boolean = True
Dim count As Int16 = 0
Dim block_count As Int16 = 0
While ((StreamIn.Peek Char > -1) And (count <> -1) And (count <
65000))
count += 1
tmp = ASCII_TO_EBCDIC (Asc(StreamIn.R eadChar))
Extract_Bits(tm p, bHigh, bLow, first_time_thro ugh)
StreamOut.Write (CStr(Hex(bHigh )) & CStr(Hex(bLow)) )
If count = 10 Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
ElseIf ((count - 10) Mod 252) = 0 Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
ElseIf (count > 2049) Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
count = 0
block_count += 1
If (block_count > 19) Then
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =
MsgBoxResult.Ca ncel) Then
'check to continue at the end of each 2K block
count = -1
End If
End If
End If
End While
StreamIn.Close( )
FileStream.Clos e()
StreamOut.Close ()
FileOutStream.C lose()

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<#I******* ******@TK2MSFTN GP10.phx.gbl>.. .
Jeff,
In addition to my other comments:
Currently it looks like the lower range (0 to 127)

Dim StreamIn As System.IO.Strea mReader = _
New System.IO.Strea mReader(FileStr eam, _
System.Text.Enc oding.GetEncodi ng("ASCII"))


Remember that ASCII has code points from 0 to 127. You could use ANSI, which
is 0 to 255, however you indicated that the file itself is EBCDIC, so you
should use an EBCDIC encoding (37 or 500) & the framework will translate the
EBCDIC text to Unicode for you. StreamIn.Read will always return an Unicode
character (-1 for end of file). StreamIn.Read will use the encoding object
to convert the code point in the file to this Unicode character.

Hope this helps
Jay

----Snip-------

Nov 20 '05 #5
Jeff,
My file contains Binary Coded Decimal only. Please define "Binary Coded Decimal", as to me "Binary Coded Decimal" or BCD
is packed fields, where each digit of a number is half of a byte. So for
example the number 1234 is represented as three bytes. &H01, &H23, &H4F,
where the F represents a positive number.

In other words BCD is number formatting that does not really entail EBCDIC
or ASCII.

EBCDIC on the other hand is a character encoding, where each byte is a
single character for example the letter "A" is represented as a single byte
with a value of &HC1.

ASCII is also a character encoding, where each byte is a single character,
for example the letter "A" is represented as a single byte with a value of
&H41.

The Asc function & the StreamReaders are used to convert text from one
character encoding to another, they should not be used on BCD aka packed
fields. They should be used on EBCDIC, ASCII and Ansi text files.

So I think we need to back up and ask what are you attempting to do:
1. Read EBCDIC text in a file?
2. Read BCD (packed numbers) in a file?
3. Read both EBCDIC text & BCD (packed numbers) in a file?

4. What is the file format look like (COBOL copy book?)
For information on EBCDIC in .NET see:
http://www.yoda.arachsys.com/csharp/ebcdic/

For information on Unicode & ASCII in .NET see:
http://www.yoda.arachsys.com/csharp/ebcdic/

Hope this helps
Jay
"Jeff" <du**********@m sn.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com... My file contains Binary Coded Decimal only. So with that in mind I
started to look into the BinaryRead. I must admit, I'm getting very
close now but it's still not 100%. I tried using just StreamIn.Read
and assign the value to a Byte field. The problem was I would get the
odd overflow error so I started to convert the byte in to an integer
using ASC.

Thoughts?

Thanks for the help, my hairline appreciates it.
Jeff

Private Sub OpenAndProcessF ile(ByVal sInputFileName As String)
Dim FileStream As System.IO.FileS tream = New
System.IO.FileS tream(sInputFil eName, _
IO.FileMode.Ope n, IO.FileAccess.R ead, IO.FileShare.Re ad)
Dim StreamIn As BinaryReader = New BinaryReader(Fi leStream)
Dim FileOutStream As FileStream = New
FileStream("tmp _out.txt", FileMode.OpenOr Create)
Dim StreamOut As StreamWriter = New
StreamWriter(Fi leOutStream)
Dim tmp As Int16
Dim bHigh, bLow As Byte
Static first_time_thro ugh As Boolean = True
Dim count As Int16 = 0
Dim block_count As Int16 = 0
While ((StreamIn.Peek Char > -1) And (count <> -1) And (count <
65000))
count += 1
tmp = ASCII_TO_EBCDIC (Asc(StreamIn.R eadChar))
Extract_Bits(tm p, bHigh, bLow, first_time_thro ugh)
StreamOut.Write (CStr(Hex(bHigh )) & CStr(Hex(bLow)) )
If count = 10 Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
ElseIf ((count - 10) Mod 252) = 0 Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
ElseIf (count > 2049) Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
count = 0
block_count += 1
If (block_count > 19) Then
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =
MsgBoxResult.Ca ncel) Then
'check to continue at the end of each 2K block
count = -1
End If
End If
End If
End While
StreamIn.Close( )
FileStream.Clos e()
StreamOut.Close ()
FileOutStream.C lose()

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message

news:<#I******* ******@TK2MSFTN GP10.phx.gbl>.. .
Jeff,
In addition to my other comments:
Currently it looks like the lower range (0 to 127)

Dim StreamIn As System.IO.Strea mReader = _
New System.IO.Strea mReader(FileStr eam, _
System.Text.Enc oding.GetEncodi ng("ASCII"))


Remember that ASCII has code points from 0 to 127. You could use ANSI, which is 0 to 255, however you indicated that the file itself is EBCDIC, so you should use an EBCDIC encoding (37 or 500) & the framework will translate the EBCDIC text to Unicode for you. StreamIn.Read will always return an Unicode character (-1 for end of file). StreamIn.Read will use the encoding object to convert the code point in the file to this Unicode character.

Hope this helps
Jay

----Snip-------

Nov 20 '05 #6
I am trying to read a file that is all BCD. It's similar to packed
fields but not quite. Each number is half of a byte but the fields are
not signed. In COBOL you would use comp-3 to define a filed as packed
decimal and because of the sign you would need 4 character positions
to hold a value of 6 digits long (one have of the 4th is used because
of the sign). In BCD every half byte holds a numeric value. In your
example below, 1234 would be represented by &H12 &H34. The file is
broken apart by using the Hex values of the EBCDIC characters on the
MVS and extracting the High and Low bits.

This is the type of file I need to read and process in Windows.

I have downloaded the file using Binary in FTP and I can read the file
in "c" on a UNIX box with no problems. In "c" I read it into a
character filed and logical and the filed with 0xF0 or 0x0F and shift
bits. This is also my objective in Visual Basic :) I'm starting off
easy with this language and believe it or not I have learned allot in
the last few weeks.


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<u$******* *******@TK2MSFT NGP12.phx.gbl>. ..
Jeff,
My file contains Binary Coded Decimal only.

Please define "Binary Coded Decimal", as to me "Binary Coded Decimal" or BCD
is packed fields, where each digit of a number is half of a byte. So for
example the number 1234 is represented as three bytes. &H01, &H23, &H4F,
where the F represents a positive number.

In other words BCD is number formatting that does not really entail EBCDIC
or ASCII.

EBCDIC on the other hand is a character encoding, where each byte is a
single character for example the letter "A" is represented as a single byte
with a value of &HC1.

ASCII is also a character encoding, where each byte is a single character,
for example the letter "A" is represented as a single byte with a value of
&H41.

The Asc function & the StreamReaders are used to convert text from one
character encoding to another, they should not be used on BCD aka packed
fields. They should be used on EBCDIC, ASCII and Ansi text files.

So I think we need to back up and ask what are you attempting to do:
1. Read EBCDIC text in a file?
2. Read BCD (packed numbers) in a file?
3. Read both EBCDIC text & BCD (packed numbers) in a file?

4. What is the file format look like (COBOL copy book?)
For information on EBCDIC in .NET see:
http://www.yoda.arachsys.com/csharp/ebcdic/

For information on Unicode & ASCII in .NET see:
http://www.yoda.arachsys.com/csharp/ebcdic/

Hope this helps
Jay
"Jeff" <du**********@m sn.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
My file contains Binary Coded Decimal only. So with that in mind I
started to look into the BinaryRead. I must admit, I'm getting very
close now but it's still not 100%. I tried using just StreamIn.Read
and assign the value to a Byte field. The problem was I would get the
odd overflow error so I started to convert the byte in to an integer
using ASC.

Thoughts?

Thanks for the help, my hairline appreciates it.
Jeff

Private Sub OpenAndProcessF ile(ByVal sInputFileName As String)
Dim FileStream As System.IO.FileS tream = New
System.IO.FileS tream(sInputFil eName, _
IO.FileMode.Ope n, IO.FileAccess.R ead, IO.FileShare.Re ad)
Dim StreamIn As BinaryReader = New BinaryReader(Fi leStream)
Dim FileOutStream As FileStream = New
FileStream("tmp _out.txt", FileMode.OpenOr Create)
Dim StreamOut As StreamWriter = New
StreamWriter(Fi leOutStream)
Dim tmp As Int16
Dim bHigh, bLow As Byte
Static first_time_thro ugh As Boolean = True
Dim count As Int16 = 0
Dim block_count As Int16 = 0
While ((StreamIn.Peek Char > -1) And (count <> -1) And (count <
65000))
count += 1
tmp = ASCII_TO_EBCDIC (Asc(StreamIn.R eadChar))
Extract_Bits(tm p, bHigh, bLow, first_time_thro ugh)
StreamOut.Write (CStr(Hex(bHigh )) & CStr(Hex(bLow)) )
If count = 10 Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
ElseIf ((count - 10) Mod 252) = 0 Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
ElseIf (count > 2049) Then
StreamOut.Write (CStr(Chr(12) & Chr(10)))
count = 0
block_count += 1
If (block_count > 19) Then
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =
MsgBoxResult.Ca ncel) Then
'check to continue at the end of each 2K block
count = -1
End If
End If
End If
End While
StreamIn.Close( )
FileStream.Clos e()
StreamOut.Close ()
FileOutStream.C lose()

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message

news:<#I******* ******@TK2MSFTN GP10.phx.gbl>.. .
Jeff,
In addition to my other comments:
> Currently it looks like the lower range (0 to 127) Dim StreamIn As System.IO.Strea mReader = _
> New System.IO.Strea mReader(FileStr eam, _
> System.Text.Enc oding.GetEncodi ng("ASCII"))

Remember that ASCII has code points from 0 to 127. You could use ANSI, which is 0 to 255, however you indicated that the file itself is EBCDIC, so you should use an EBCDIC encoding (37 or 500) & the framework will translate the EBCDIC text to Unicode for you. StreamIn.Read will always return an Unicode character (-1 for end of file). StreamIn.Read will use the encoding object to convert the code point in the file to this Unicode character.

Hope this helps
Jay

----Snip-------

Nov 20 '05 #7
Hi Jeff,

Just a little thing, it has always been that when you have to convert files
from EBCDIC to ASCII, you should not go in the packed decimal part. That is
is not EBCDIC, that is IBM mainframe(360/370).

Better is as I thought already Jay stated to bring it back first to straight
EBCDIC and use when needed an extra byte for the plus or minus, which is as
well not standardized in EBCDIC.

Just a little thing

Cor
Nov 20 '05 #8
Jeff,
In your
example below, 1234 would be represented by &H12 &H34. The file is
broken apart by using the Hex values of the EBCDIC characters on the
MVS and extracting the High and Low bits. As I have attempted to state. If you only have BCD, then you do not have
EBCDIC characters, you have BCD digits. You have a file of Bytes or Binary.

When you FTP'ed the file, did you use binary mode? If you didn't you should
as this will avoid unnecessary converting your BCD digits to/from ANSI (the
bytes are binary, leave them binary!).

Stay away from Asc, AscW & StreamReaders! As they are attempting to convert
your binary into characters on you.

With C & C++ on Unix a byte and a char are largely treated as the same
thing, with in .NET a Byte is an 8 bit value, in your case this 8 bit value
has 2 digits in it. In .NET a Char is a 16 bit value. Which can be converted
to & from a Byte via an Encoding... One should only use an encoding to
convert a byte if the byte has an encoded character in it! (such as an
EBCDIC "A"). I would avoid encoding if your byte has binary data in it (such
as BCD, Packed Decimal, Integer, Single, Double) In other words .NET has a
clearer definition of Binary files (Stream & BinaryReader) and Text Files
(StreamReader). Note that a Binary file could actually have both Binary data
& Text data, which is where the BinaryReader is useful. Seeing as
BinaryReader has no real support of BCD I would not use it in this case,
BinaryReader is useful if I had Integer, Single and Double fields, along
with Strings, in a file & I wanted to read the different values, the
BinaryReader will read the Bytes in the file and return an Integer for me
(via BinaryReader.Re adInt32). In case you were wondering: BinaryReader using
System.BitConve rt.ToInt32 to convert (reassembly really) the bytes in the
file to an Integer.

Now that we know what your input looks like, what do you want your output to
look like?

It appears to be hex digits as text separated by end of line, correct?

What is the significance of &HC2? In EBCDIC its the letter "B". I noticed
its missing in current versions..

Given you have Binary in and Text out I would use something like:

Dim input As New System.IO.FileS tream(sInputFil eName, _
IO.FileMode.Ope n, IO.FileAccess.R ead, IO.FileShare.Re ad)
Dim output As New StreamWriter(ou tputPath, False, Encoding.Defaul t)
Dim value As Integer
Dim bHigh, bLow As Byte
Dim count As Integer = 0
Dim block_count As Integer = 0
value = input.ReadByte( )
While ((value > -1) And (count <> -1) And (count < 65000))
count += 1
'bHigh = CByte((value >> 4) And &HFF)
'bLow = CByte(value And &HFF)
'output.Write(H ex(bHigh) & Hex(bLow))
output.Write(va lue.ToString("X 2"))
If count = 10 Then
output.WriteLin e()
ElseIf ((count - 10) Mod 252) = 0 Then
output.WriteLin e()
ElseIf (count > 2049) Then
output.WriteLin e()
count = 0
block_count += 1
If (block_count > 19) Then
If (MsgBox("Contin ue", MsgBoxStyle.OKC ancel) =
MsgBoxResult.Ca ncel) Then
'check to continue at the end of each 2K block
count = -1
End If
End If
End If
value = input.ReadByte( )
End While
input.Close()
output.Close()

Note that the follow lines are equivalent:

bHigh = CByte((value >> 4) And &HF)
bLow = CByte(value And &HF)
output.Write(He x(bHigh) & Hex(bLow))

output.Write(va lue.ToString("X 2"))
Hope this helps
Jay

"Jeff" <du**********@m sn.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com... I am trying to read a file that is all BCD. It's similar to packed
fields but not quite. Each number is half of a byte but the fields are
not signed. In COBOL you would use comp-3 to define a filed as packed
decimal and because of the sign you would need 4 character positions
to hold a value of 6 digits long (one have of the 4th is used because
of the sign). In BCD every half byte holds a numeric value. In your
example below, 1234 would be represented by &H12 &H34. The file is
broken apart by using the Hex values of the EBCDIC characters on the
MVS and extracting the High and Low bits.

This is the type of file I need to read and process in Windows.

I have downloaded the file using Binary in FTP and I can read the file
in "c" on a UNIX box with no problems. In "c" I read it into a
character filed and logical and the filed with 0xF0 or 0x0F and shift
bits. This is also my objective in Visual Basic :) I'm starting off
easy with this language and believe it or not I have learned allot in
the last few weeks.

<<snip>>
Nov 20 '05 #9
Reading the file in using filestream works. I missed the part in my
book where it said filestream could be used for binary files and kept
using binaryread and streamread.

Many Thanks for the help.

As a side note "C1" or "C2" etc are values that may be used in flag
indicators and are populated by the hardware. That's the reason I need
this level of detail.
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
11854
by: Peter Galfi | last post by:
Hi! I am looking for a library in Python that would read PDF files and I could extract information from the PDF with it. I have searched with google, but only found libraries that can be used to write PDF files. Any ideas? Peter
1
1381
by: John Puopolo | last post by:
All, Is there a class in the .NET framework specifically designed for reading configuration files, e.g., MyApp.exe.config? I know that I can read them via XmlReader and the like, but I was wondering if there were classes with a slightly higher level of abstraction. Thanks, John
1
3032
by: Manjunath sp via DotNetMonster.com | last post by:
Hi, How to effectively write and read structures from binary files in .Net? Currently I am using functions like ReadInt32 and the likes to read data from binary file into each elements of a structure, but this is very inefficent compared to VB3 where I could use Get method to read the whole structure data? Is there any similar method in .net?...
1
2122
by: Shmuel Shulman | last post by:
Hi I have 2 funny probs that are probably related 1. I can't read the entire field from a dbf file it cuts it at some point (see below) 2. When I use Access or SQL server to read these files they too read only up to that point also SQLServer sets the number characters to the 1 that it read
2
1778
by: eddieb7 | last post by:
Hi, I am new to visual Studio 2005 C++ and am looking for some directions on where best to start. I come from a mainly Delphi background and looking to switch to VS 2005 C++ or C#. I am looking to write a test utility that would read several Flat Files and allow me to produce
0
2182
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under...
10
3162
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa Rattus_norvegicus.fa (They are FA files that can be opened in any text editor.)
2
2229
by: doublemaster007 | last post by:
Hi How to read binary files in MAC OS? FILE *readFile = fopen("filename", "rb"); then reading it like this: count=fread(readBuffer, sizeof(char), bufferSize, readFile)
0
1455
by: philipdv | last post by:
I have a process reading xml files over an https connection My code to read this xml file is CErrorAndLog.LogAlways("CKlim", "parseXML", "Parsing XML file: " + strFileName) m_xmld = New XmlDocument m_xmld.XmlResolver = Nothing m_xmld.Load(strFileName) Where strFileName is somthing like...
1
7625
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6219
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5476
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5193
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3621
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2073
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1187
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.