0

I need to get the EBCDIC value of a character in C. I don't know how. Do I have to get the ASCII value first then get the EBCDIC value from there? Thanks anyone

1 Answer 1

3

If you're on a system that uses EBCDIC as the character encoding, you already have it:

char xyzzy = 'A'; // xyzzy is now 0xc1

If your environment is an ASCII one and you simply want the EBCDIC code point, you can just use a lookup table built from both tables, like:

enter image description here enter image description here

A lookup tables for a system using 8-bit ASCII characters to give you the EBCDIC code points would be something like:

int ebcdicCodePont (unsigned char asciiVal) {
    static int lookup[] = {
        /* 0x00-07 */   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
        /* 0x08-0f */   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
        :
        /* 0x20-27 */ 0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d,
        :
        /* 0x48-4f */ 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
        :
        /* 0x78-7f */ 0xa7, 0xa8, 0xa9,   -1, 0x45,   -1,   -1, 0x07,
    };
    if (asciiVal > 0x7f)
        return -1;
    return lookup[asciiVal];
};
2
  • If the system used EBCDIC, would char typically/always an unsigned char? Feb 26, 2014 at 7:00
  • thanks @paxdiablo. could you explain to me furthermore how to use the lookup table? I don't really get how it works.
    – entendezEJ
    Feb 26, 2014 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.