Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Read an EBCDIC File in VB.NET

0.00/5 (No votes)
13 Dec 2008 1  
This article demonstrates how to read an EBCDIC file in VB.NET, translate it to ASCII and write it out to a file.

Introduction

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!

Background

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.

''' <summary>
''' Translates a file from EBCDIC to ASCII.
''' </summary>
''' <param name="sourceEbcdicFilePath">The full path of the EBCDIC file.</param>
''' <param name="newAsciiFilePath">The full path of the new ASCII file 
''' that will be created.</param>
''' <remarks></remarks>
Private Sub TranslateFile(ByVal sourceEbcdicFilePath As String, _
                          ByVal newAsciiFilePath As String)

    'Set the encoding to the EBCDIC code page.
    Dim encoding As System.Text.Encoding = _
                    System.Text.Encoding.GetEncoding(37)

    'Set this to the length of an EBCDIC line or block.
    Dim lineLength As Integer = 134

    'Buffer used to store characters read in from the statement input file.
    Dim buffer(lineLength - 1) As Char

    'Open the EBCDIC file for reading using the EBCDIC encoding.
    Dim reader As New IO.StreamReader(sourceEbcdicFilePath, encoding)

    'Open a file to write out the data to.
    Dim writer As New IO.StreamWriter(newAsciiFilePath, _
                                      False, System.Text.Encoding.Default)

    'This is a string to store the translated line.
    Dim strAscii As String = String.Empty

    'This variable increments every time a block of data is read
    Dim iLoops As Integer = 0

    'Loop through the EBCDIC file.
    Do Until reader.EndOfStream = True

        'Read in a block of data from the EBCDIC file.
        reader.ReadBlock(buffer, 0, lineLength)

        'Translate the string using the EBCDIC encoding.
        strAscii = encoding.GetString(encoding.GetBytes(buffer))

        'Write the translated string out to a file.
        writer.WriteLine(strAscii)

        'Only call DoEvents every 1000 loops (increases performance)
        iLoops += 1
        If iLoops = 1000 Then

            Application.DoEvents()

            iLoops = 0 'reset

        End If

    Loop

    'Close files.
    reader.Close()
    writer.Close()

    'Release resources.
    reader.Dispose()
    writer.Dispose()

End Sub

Conclusion

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 string.

Happy translating!

VBRocks.

History

  • 13th December, 2008: Initial post 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here