Reputation: 29
I've been trying to convert a string containing EBCDIC characters to ASCII, this is my code so far:
string data = "F2F1F0F2F2F5F4";
Encoding ascii = Encoding.ASCII;
Encoding ebcdic = Encoding.GetEncoding("IBM037");
byte[] ebcdicData = ebcdic.GetBytes(data);
// Convert to ASCII
byte[] ebcdicDataConverted = Encoding.Convert(ebcdic, ascii, ebcdicData);
string sample = ascii.GetString(ebcdicDataConverted);
But I was expecting that the variable sample
contained this value: 2102254
Instead, it shows the same value as data
F2F1F0F2F2F5F4
Maybe I'm not understanding how this works, or I'm just burnt out, this page contains the conversion table that:
translates 8-bit EBCDIC characters to 7-bit ASCII
Is the Enconding that I'm using the right one? Am I doing something wrong?
Thanks
Upvotes: 0
Views: 799
Reputation: 29
I converted de string
to byte
using a for
like this:
byte[] ebcdicData = new byte[data.Length / 2];
for (int i = 0; i < ebcdicData.Length; i++)
{
string chunk = data.Substring(i * 2, 2);
ebcdicData[i] = Convert.ToByte(chunk, 16);
}
This way it worked. Idk why GetBytes()
does something different, I will take a look at the documentation
Upvotes: 0