Trying to figure out how to read an EBCDIC file and translate it to ASCII can be a bit of a headache. But now, thanks to .NET, you don't have to worry about headaches or sleepless nights!
The reason I wanted to write this article is because I am tasked with upgrading over 50 VB6 projects to VB.NET 2008. 7 of the VB6 projects that are being upgraded process EBCDIC files. The VB6 methods that were used to translate the files were horrendous! I played around with .NET's file encodings and figured out how to translate the EBCDIC files very easily!
Using the Code
Here is the complete source code for translating a file from EBCDIC to ASCII. It's only 1 function that can be called and passed 2 arguments: the EBCDIC file path, and the new ASCII file path that will be created and loaded with the translated content.
Private Sub TranslateFile(ByVal sourceEbcdicFilePath As String, _
ByVal newAsciiFilePath As String)
Dim encoding As System.Text.Encoding = _
Dim lineLength As Integer = 134
Dim buffer(lineLength - 1) As Char
Dim reader As New IO.StreamReader(sourceEbcdicFilePath, encoding)
Dim writer As New IO.StreamWriter(newAsciiFilePath, _
Dim strAscii As String = String.Empty
Dim iLoops As Integer = 0
Do Until reader.EndOfStream = True
reader.ReadBlock(buffer, 0, lineLength)
strAscii = encoding.GetString(encoding.GetBytes(buffer))
iLoops += 1
If iLoops = 1000 Then
iLoops = 0
Reading EBCDIC files is made easy! I hope it's as helpful to you as it is to me!
The main thing to remember is creating the EBCDIC encoding, and using it when you open the EBCDIC file for reading, and when you convert the
- 13th December, 2008: Initial post