Click here to Skip to main content
13,086,019 members (80,714 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 13 Dec 2008

How to Read an EBCDIC File in VB.NET

, 13 Dec 2008
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.


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.

''' <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 = _

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

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


            iLoops = 0 'reset

        End If


    'Close files.

    'Release resources.

End Sub


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!



  • 13th December, 2008: Initial post 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

CS Rocks
Software Developer DataPrint, LLC
United States United States

You may also be interested in...


Comments and Discussions

QuestionPacked Files? Pin
Bryan Tubbs2-Feb-10 9:47
memberBryan Tubbs2-Feb-10 9:47 
AnswerRe: Packed Files? Pin
virbhadrasutar10-Nov-10 22:43
membervirbhadrasutar10-Nov-10 22:43 
GeneralRe: Packed Files? Pin
VB Rocks11-Nov-10 5:33
memberVB Rocks11-Nov-10 5:33 
GeneralRe: Packed Files? [modified] Pin
virbhadrasutar11-Nov-10 23:54
membervirbhadrasutar11-Nov-10 23:54 
GeneralHi Pin
Irwan Hassan17-Dec-08 15:21
memberIrwan Hassan17-Dec-08 15:21 
GeneralRe: Hi Pin
VB Rocks19-Dec-08 6:28
memberVB Rocks19-Dec-08 6:28 
GeneralRe: Hi Pin
Irwan Hassan21-Dec-08 19:48
memberIrwan Hassan21-Dec-08 19:48 
GeneralRe: Hi Pin
VB Rocks22-Dec-08 11:58
memberVB 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 13 Dec 2008
Article Copyright 2008 by CS Rocks
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid