Convert EBCDIC to ASCII and vice versa
-
Hi, In my C# application, I need to convert EBCDIC data to ASCII. This EBCDIC data is stored as BLOB type in Teradata. This data also has packed decimals in it. I tried the code i found on internet, which simply converts EBCDIC to ASCII. In that there a interger array of 256 characters only. But the characters which i need to convert, char chrItem = Convert.ToChar(strEbcidic.Substring(i, 1)); returns number much higher then 256. So i get 'index out of bound of array exception'. The EBCDIC characters include 'ÁõÁõöôùô@@@@ðóùòùùðñ@ððððð' also. Could any one please help me out here? Thanks, Deepa
-
Hi, In my C# application, I need to convert EBCDIC data to ASCII. This EBCDIC data is stored as BLOB type in Teradata. This data also has packed decimals in it. I tried the code i found on internet, which simply converts EBCDIC to ASCII. In that there a interger array of 256 characters only. But the characters which i need to convert, char chrItem = Convert.ToChar(strEbcidic.Substring(i, 1)); returns number much higher then 256. So i get 'index out of bound of array exception'. The EBCDIC characters include 'ÁõÁõöôùô@@@@ðóùòùùðñ@ððððð' also. Could any one please help me out here? Thanks, Deepa
I think the problem may be that your input is not EBCDIC - ebcdic is an 8-bit code so only has characters 000 to 255. Wiki on EBCDIC[^]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
I think the problem may be that your input is not EBCDIC - ebcdic is an 8-bit code so only has characters 000 to 255. Wiki on EBCDIC[^]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
Hi, Yes, none of these characters in the file are in the 255 codes of EBCDIC. As i had mentioned earlier, the file from the BLOB, contains packed decimal also. Are you sure its not EBCDIC, when i do .ToCharArray() i get like: [0]: 22909 '好' [1]: 8224 '†' [2]: 8256 '⁀' [3]: 8592 '←' [4]: 879 'ͯ' [5]: 12629 'ㅕ' [6]: 16527 '䂏' [7]: 16448 '䁀' [8]: 16448 '䁀' [9]: 16448 '䁀' [10]: 16448 '䁀' [11]: 16448 '䁀' [12]: 16448 '䁀' [13]: 65533 '�' [14]: 49605 '쇅' [15]: 16601 '䃙' [16]: 16448 '䁀' [17]: 16448 '䁀' [18]: 16448 '䁀' [19]: 16448 '䁀' [20]: 16448 '䁀' [21]: 16448 '䁀' [22]: 16448 '䁀' [23]: 62448 '' [24]: 63216 '' [25]: 63220 '' [26]: 63473 '' [27]: 63216 '' [28]: 62451 '' [29]: 61689 '' [30]: 54769 '헱' [31]: 16448 '䁀' [32]: 16448 '䁀' [33]: 16448 '䁀' [34]: 16448 '䁀' So their character codes are greater then 255. How to resolve this?
-
Hi, Yes, none of these characters in the file are in the 255 codes of EBCDIC. As i had mentioned earlier, the file from the BLOB, contains packed decimal also. Are you sure its not EBCDIC, when i do .ToCharArray() i get like: [0]: 22909 '好' [1]: 8224 '†' [2]: 8256 '⁀' [3]: 8592 '←' [4]: 879 'ͯ' [5]: 12629 'ㅕ' [6]: 16527 '䂏' [7]: 16448 '䁀' [8]: 16448 '䁀' [9]: 16448 '䁀' [10]: 16448 '䁀' [11]: 16448 '䁀' [12]: 16448 '䁀' [13]: 65533 '�' [14]: 49605 '쇅' [15]: 16601 '䃙' [16]: 16448 '䁀' [17]: 16448 '䁀' [18]: 16448 '䁀' [19]: 16448 '䁀' [20]: 16448 '䁀' [21]: 16448 '䁀' [22]: 16448 '䁀' [23]: 62448 '' [24]: 63216 '' [25]: 63220 '' [26]: 63473 '' [27]: 63216 '' [28]: 62451 '' [29]: 61689 '' [30]: 54769 '헱' [31]: 16448 '䁀' [32]: 16448 '䁀' [33]: 16448 '䁀' [34]: 16448 '䁀' So their character codes are greater then 255. How to resolve this?
The problem is that chars in .NET are Unicode, which is generally two bytes. Using a byte array instead of a char array might solve your problem.
-
Hi, In my C# application, I need to convert EBCDIC data to ASCII. This EBCDIC data is stored as BLOB type in Teradata. This data also has packed decimals in it. I tried the code i found on internet, which simply converts EBCDIC to ASCII. In that there a interger array of 256 characters only. But the characters which i need to convert, char chrItem = Convert.ToChar(strEbcidic.Substring(i, 1)); returns number much higher then 256. So i get 'index out of bound of array exception'. The EBCDIC characters include 'ÁõÁõöôùô@@@@ðóùòùùðñ@ððððð' also. Could any one please help me out here? Thanks, Deepa
I highly recommend using Jon Skeet's EBCDIC encoding library[^] We have been using this for years. You basically just set up Encoding objects and pass them into Stream objects. Granted, this is VB.NET code, but it should still make sense:
Dim inputEncoding As Encoding = EbcdicEncoding.GetEncoding("EBCDIC-US")
Dim outputEncoding = Encoding.ASCII
Dim inputReader As StreamReader
Dim outputWriter As StreamWriter
inputReader = New StreamReader(inputFile, inputEncoding)
outputWriter = New StreamWriter(outputFile, False, outputEncoding)I don't claim to be a know it all, for I know that I am not...
I usually have an answer though.
-
I highly recommend using Jon Skeet's EBCDIC encoding library[^] We have been using this for years. You basically just set up Encoding objects and pass them into Stream objects. Granted, this is VB.NET code, but it should still make sense:
Dim inputEncoding As Encoding = EbcdicEncoding.GetEncoding("EBCDIC-US")
Dim outputEncoding = Encoding.ASCII
Dim inputReader As StreamReader
Dim outputWriter As StreamWriter
inputReader = New StreamReader(inputFile, inputEncoding)
outputWriter = New StreamWriter(outputFile, False, outputEncoding)I don't claim to be a know it all, for I know that I am not...
I usually have an answer though.
-
Try This #region public static byte[] ConvertAsciiToEbcdic(byte[] asciiData) public static byte[] ConvertAsciiToEbcdic(byte[] asciiData) { // Create two different encodings. Encoding ascii = Encoding.ASCII; Encoding ebcdic = Encoding.GetEncoding("IBM037"); //Retutn Ebcdic Data return Encoding.Convert(ascii, ebcdic, asciiData); } #endregion #region public static byte[] ConvertEbcdicToAscii(byte[] ebcdicData) public static byte[] ConvertEbcdicToAscii(byte[] ebcdicData) { // Create two different encodings. Encoding ascii = Encoding.ASCII; Encoding ebcdic = Encoding.GetEncoding("IBM037"); //Retutn Ascii Data return Encoding.Convert(ebcdic, ascii, ebcdicData); } #endregion