none
ASCII-to-EBCDIC or EBCDIC-to-ASCII RRS feed

  • Question

  • The question was 'how do you convert ASCII to EBCDIC?', but both of the programs told what to in the other situation of what to do to convert EBCDIC to ASCII.  But I don't need those, how would one convert ASCII to EBCDIC?

    Thursday, January 23, 2020 7:27 PM

All replies

  • You first convert from ASCII or EBCDIC (stored in an array of bytes, which is the only way that you can represent them properly in .NET) into a String (which is Unicode) and then from the Unicode string into the other encoding. Both conversions can be done with System.Text.Encoding.

    using System.Text;
    //...
    
    // From ASCII to EBCDIC
    byte[] ascii = ... // Initialize to your ASCII bytes
    string unicode = Encoding.ASCII.GetString(ascii);
    byte[] ebcdic = Encoding.GetEncoding(500).GetBytes(unicode);
    
    // From EBCDIC to ASCII
    byte[] ebcdic = ... // Initialize to your EBCDIC bytes
    string unicode = Encoding.GetEncoding(500).GetString(ebcdic);
    byte[] ascii = Encoding.ASCII.GetBytes(unicode);
    

    Note that I have used Encoding 500 for EBCDIC. There are actually several international variations of EBCDIC; you can find the numeric codes here: https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.8

    The international variations will convert correctly to/from Unicode, but the latter will not convert to ASCII since this does not accept the international characters.

    Thursday, January 23, 2020 8:12 PM
    Moderator
  • I'm curious to know what use you have for EBCDIC.  They only time I've encountered it (after leaving the mainframe world) is in the SEG-Y standard used for recording seismic data.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, January 24, 2020 7:59 AM