tags:

views:

440

answers:

1

Hello,

I am using the standard .NET OdbcConnection to connect to an AS/400 iSeries database. I am able to query this database. Unfortunately, there are some fields that appear to be encoded. How do I decode these values in C#? I have tried the following:

string text = string.Empty;
if (bytes.Length > 0)
{
    ASCIIEncoding encoder = new ASCIIEncoding();
    text = encoder.GetString(bytes);
}
return text;

The bytes variable represents the data that needs to be decoded. Unfortunately, I am not having any luck. I have been told that the data will return correctly if I setup an ODBC data source on my Windows machine and check the "Convert binary data (CCSID65535) to text" checkbox in the translation tab. However, I want to use pure C#. Any ideas? Am I way off?

Thanks!

+7  A: 

Chances are it's using EBCDIC. You could try using Encoding.GetEncoding(37) or you could use the EBCDIC encoding I wrote a while ago.

Jon Skeet
Beat me to it, Jon. EBCDIC was my first thought too.
Kevin
You're the man. This removed my headache big time. Thank you so much for your help!
Would suggest using one of the names ("IBM037" or similar see http://msdn.com/System.Text.Encoding for a full list) as this avoids the magic number.
Richard
@Richard: It changes a magic number into a magic string :) It would be nice if these were exposed via the Encoding API...
Jon Skeet
@Jon: Like Encoding.GetEncodings "Returns an array containing all encodings"?
Richard