EBCDIC to ASCII using PowerShell (or command line portable Application)

Answered EBCDIC to ASCII using PowerShell (or command line portable Application)

  • Tuesday, January 22, 2013 5:44 PM
     
     

    I am looking for a way to convert an EBCDIC file to an ASCII file using PowerShell.  It can be PowerShell using another program but that program would need to be a portable application that can be run from the command line (Portable Application as defined on Wikipedia) so that PowerShell can activate the conversion.  Thank you everyone.  -Dan


    Dan

All Replies

  • Tuesday, January 22, 2013 6:26 PM
     
     

    Google is a wonderful tool. Try this search phrase:

    ebcdic to ASCII converter

  • Tuesday, January 22, 2013 6:44 PM
     
     

    I guess I need to be a little more specific.  I have spent 20+ hours looking at Google/Bing trying to figure it out and have not been able to come up with a strong answer.  I was (am) hoping somebody out there has run into this problem and knew a good way to automate it.  I am a little insulted that the first answer I got was Google it.  Should I add that I have already Goggled it in any questions I have from now on? 

    I will also note that I have searched this forum and several others and have not found any questions / responses that tackle this question.

    Thank you,

    -Dan


    Dan

  • Tuesday, January 22, 2013 7:19 PM
     
     

    I am a little insulted that the first answer I got was Google it.  Should I add that I have already Goggled it in any questions I have from now on?

    Beats me why you should feel insulted. You gave no indication whatsoever about the work you had already done in an attempt to solve your problem, thus forcing respondents to do the groundwork for you. Yes, mentioning your Google searches would not only have been an excellent idea but actually a basic prerequisite. A good post is a post that states the facts, explains the work done to date plus a concise question.

    This link gives the complete VBA code for a converter. You could convert it to VBScript with a minimal effort, then run it as a command line tool. Note also that there are three flavours of EBCDIC.

  • Tuesday, January 22, 2013 7:48 PM
     
     

    Thank you for the link.  Unfortunately I had already fount it and the server I am running the PowerShell script on will not allow VBA scripts to be run.  I can get permission to run a tested portable application but not a script outside of PowerShell.

    I need to apologize.  Although I use several forums for other programs they are all at a higher level only and they want you to simply ask the question, give sample code if you have it and any theories if you have them and it is assumed that you did all the basic ground work, Google, going through your books, asking co-workers, checking all the forums that you know before you ask your question.  I have even been asked before to put less information into my post, things like “I checked Google” and “looked in the books” so that people can get to the question faster.  I will keep this in mind when using this forum in the future.

    Again, I really do appreciate any help I can get on this.

    Thank you, -Dan


    Dan

  • Tuesday, January 22, 2013 8:12 PM
     
     

    I need to apologize.

    No problem. The code we both found is actually quite simple. It consists of a couple of look-up tables and a hex converter. If you know PowerShell (which I don't) then a translation is straightforward. If you don't then perhaps you're lucky and someone in this forum will fill an idle half-hour by showing you how it's done.
  • Tuesday, January 22, 2013 8:14 PM
    Moderator
     
     

    Hi,

    Indeed that code is VBA, but you should be able to translate it to VBScript with minimal effort. The code could also be translated to PowerShell.

    Bill

  • Tuesday, January 22, 2013 9:02 PM
     
     Answered Has Code

    PowerSHell has a

     builtin EBCDIC converter - well ... dotNet has a builtin EBCDIC converter.

    #http://www.codeproject.com/Articles/31720/How-to-Read-an-EBCDIC-File-in-VB-NET
    $fi=[io.fileinfo]"$pwd\Demo-CheckedListBox.txt"
    $reader=$fi.OpenRead()
    $length=$reader.Length
    $buffer=New-Object byte[] $length
    $reader.Read($buffer,0,$length)
    $enc=[System.Text.Encoding]::GetEncoding(37)
    $enc.GetString($enc.GetBytes($buffer))


    ¯\_(ツ)_/¯

  • Tuesday, January 22, 2013 9:20 PM
     
      Has Code

    Here is the full file-to-file solution from the codeplex example:

    # code adapted from -> http://www.codeproject.com/Articles/31720/How-to-Read-an-EBCDIC-File-in-VB-NET
    # By Gary Lima
    # Adapted by Jim Vierra http://tech-comments.blogspot.com
    $code=@'
    Imports System.IO
    Imports System
    Public CLass EBCDICUtils
    Shared 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
    End CLass
    '@
    Add-Type $code -Language VisualBasic
    [ebcdicutils]::TranslateFile($sourceEbcdicFilePath, $newAsciiFilePath)


    ¯\_(ツ)_/¯