Click here to Skip to main content
Click here to Skip to main content

How to Read an EBCDIC File in VB.NET

, 13 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

CS Rocks
Software Developer DataPrint, LLC
United States United States

Comments and Discussions

 
QuestionPacked Files? PinmemberBryan Tubbs2-Feb-10 9:47 
AnswerRe: Packed Files? Pinmembervirbhadrasutar10-Nov-10 22:43 
GeneralRe: Packed Files? PinmemberVB Rocks11-Nov-10 5:33 
GeneralRe: Packed Files? [modified] Pinmembervirbhadrasutar11-Nov-10 23:54 
GeneralHi PinmemberIrwan Hassan17-Dec-08 15:21 
GeneralRe: Hi PinmemberVB Rocks19-Dec-08 6:28 
GeneralRe: Hi PinmemberIrwan Hassan21-Dec-08 19:48 
GeneralRe: Hi PinmemberVB Rocks22-Dec-08 11:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 13 Dec 2008
Article Copyright 2008 by CS Rocks
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid