Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / Visual Basic
Article

How to Read an EBCDIC File in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.25/5 (5 votes)
13 Dec 2008CPOL1 min read 58.2K   17   8
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.

VB.NET
''' <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)


Written By
Software Developer DataPrint, LLC
United States United States

Comments and Discussions

 
QuestionPacked Files? Pin
Bryan Tubbs2-Feb-10 9:47
Bryan Tubbs2-Feb-10 9:47 
AnswerRe: Packed Files? Pin
virbhadrasutar10-Nov-10 22:43
virbhadrasutar10-Nov-10 22:43 
GeneralRe: Packed Files? Pin
CS Rocks11-Nov-10 5:33
CS Rocks11-Nov-10 5:33 
GeneralRe: Packed Files? [modified] Pin
virbhadrasutar11-Nov-10 23:54
virbhadrasutar11-Nov-10 23:54 
GeneralHi Pin
Irwan Hassan17-Dec-08 15:21
Irwan Hassan17-Dec-08 15:21 
GeneralRe: Hi Pin
CS Rocks19-Dec-08 6:28
CS Rocks19-Dec-08 6:28 
GeneralRe: Hi Pin
Irwan Hassan21-Dec-08 19:48
Irwan Hassan21-Dec-08 19:48 
GeneralRe: Hi Pin
CS Rocks22-Dec-08 11:58
CS Rocks22-Dec-08 11:58 

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

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