Thread: EBCDIC to ASCII

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    EBCDIC to ASCII

    hi all ... im trying to make a ebcdic to ascii conversion function, however something is not
    working right. I keep getting characters showing up like ...

    Code:
     ( : / - / )€                 -  ( :  %)€                                      
        -  ( :  %, . :  )€                    ( -  : / - / )€              -- (): - 
    -  , :  -   €      - €                                                   :     €
                                                                          €€€€€€€€€€
    €€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€µ€€€„€€€€€€€€€€€€€€€€€
    €€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€€
    Just wondering if anyone might have some insight on this ... here is the code ...

    Code:
            const int NUMOFROWS = 40;
            const int NUMOFCOLS = 80;
            StringBuilder buffer = new StringBuilder();
            private char[] ASCIIHeader = new char[3200];
            
            private byte[] EBCDICtoASCII =
                { 0, 1, 2, 3, 156, 9, 134, 127, 151, 141,
                142, 11, 12, 13, 14, 15, 16, 17, 18, 19, 157,
                133, 8, 135, 24, 25, 146, 143, 28, 29, 30, 31,
                128, 129, 130, 131, 132, 10, 23, 27, 136, 137, 138,
                139, 140, 5, 6, 7, 144, 145, 22, 147, 148, 149,
                150, 4, 152, 153, 154, 155, 20, 21, 158, 26, 32,
                160, 161, 162, 163, 164, 165, 166, 167, 168, 91, 46,
                60, 40, 43, 33, 38, 169, 170, 171, 172, 173, 174,
                175, 176, 177, 93, 36, 42, 41, 59, 94, 45, 47,
                178, 179, 180, 181, 182, 183, 184, 185, 124, 44, 37,
                95, 62, 63, 186, 187, 188, 189, 190, 191, 192, 193,
                194, 96, 58, 35, 64, 39, 61, 34, 195, 97, 98,
                99, 100, 101, 102, 103, 104, 105, 196, 197, 198, 199,
                200, 201, 202, 106, 107, 108, 109, 110, 111, 112, 113,
                114, 203, 204, 205, 206, 207, 208, 209, 126, 115, 116,
                117, 118, 119, 120, 121, 122, 210, 211, 212, 213, 214,
                215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
                226, 227, 228, 229, 230, 231, 123, 65, 66, 67, 68,
                69, 70, 71, 72, 73, 232, 233, 234, 235, 236, 237,
                125, 74, 75, 76, 77, 78, 79, 80, 81, 82, 238,
                239, 240, 241, 242, 243, 92, 159, 83, 84, 85, 86,
                87, 88, 89, 90, 244, 245, 246, 247, 248, 249, 48,
                49, 50, 51, 52, 53, 54, 55, 56, 57, 250, 251,
                252, 253, 254, 255 };
    
    
            private void ConvertEBCtoASCII(char[] ASCIIHeader)
            {
                for (int row = 0; row < NUMOFROWS; row++)
                {
                    for (int col = 0; col < NUMOFCOLS; col++)
                    {
                        buffer.Append( Convert.ToChar( EBCDICtoASCII[ ASCIIHeader[ (row) * NUMOFCOLS + col ] ] ) );
                    }
                    buffer.AppendLine();
                }
                outputFileTextBox.Text = Convert.ToString(buffer);
            }

    any help would be great !


  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Your "translation" table just doesn't look right to me. You may want to review this table.

    Also, assuming that you may be pulling data from a midrange (AS400) or mainframe (Sytem 390's) keep in mind that some data may be in Comp3 or packed decimal format which is a binary field that "packs" two digits into each byte using a notation called binary coded decimal. You'll have to "unpack" that data prior to translating.

  3. #3
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    hi ... thanks for the reply/input bob ... however im quite sure the byte array is done properly, and there is no packed data involved. Im trying to convert a EBCIDIC header that is present on SEGY seismic data, that consists of 3200 bytes. Im not sure if the StringBuilder or the Convert statmemts have anything to do with it ...

    Any other input would be dynamite !

    Thanks

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Hopefully, you'll humor me here a little bit and try the following snippet. I believe the Encoding.GetEncoding(37) may resolve your problem

    Code:
    using System;
    using System.IO;
    using System.Text;
    
    public class ConvertFile
    {
        public static void Main(string[] args)
        {
              string inputFile = "bob.Txt"; // Input SEGY file
            string outputFile = "out.txt"; //  Output ASCII file
            try
            {
                // Create the reader and writer with appropriate encodings.
                using (StreamReader inputReader =
                    new StreamReader(inputFile, Encoding.GetEncoding(37)))
                {
                    using (StreamWriter outputWriter =
                        new StreamWriter(outputFile))
                    {
                        char[] buffer = new char[3200];
                        inputReader.Read(buffer, 0, buffer.Length);
                        outputWriter.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("Exception during processing: {0}", e.Message);
            }
        }
    }

  5. #5
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    WOW ! Thanks for the reply Bob ! i quickly tried that out, and it works awesome ! I'm new to C# and had no idea about Encoding ... thanks again.


  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    1

    Cool Convert EBCDI

    well you could try using the encoding class with encoding.convert , eg:

    Dim source As Encoding = Encoding.GetEncoding(37)'///EBCDIC encoding.
    Dim path As Encoding = Encoding.ASCII
    Dim b As Byte() = path.GetBytes(TextBox1.Text.ToCharArray)
    Dim aB As Byte() = Encoding.Convert(source, path, b)

    Console.WriteLine(path.GetString(aB))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  3. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  4. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM
  5. EBCDIC files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 03:37 PM