C/C++ : EBCDIC to Ascii Converter

ASCII is the American Standard Code for Information Interchange, also known as ANSI X3.4. There are many variants of this standard, typically to allow different code pages for language encoding, but they all basically follow the same format. ASCII is quite elegant in the way it represents characters, and it is very easy to write code to manipulate upper/lowercase and check for valid data ranges.
ASCII is essentially a 7-bit code which allows the 8th most significant bit (MSB) to be used for error checking, however most modern computer systems tend to use ASCII values of 128 and above for extended character sets.

EBCDIC (Extended Binary Coded Decimal Interchange Code) is a character encoding set used by IBM mainframes. Unlike virtually every computer system in the world which uses a variant of ASCII, IBM mainframes and midrange systems such as the AS/400 tend to use a wholly incompatible character set primarily designed for ease of use on punched cards. EBCDIC uses the full 8 bits available to it, so parity checking cannot be used on an 8 bit system. Also, EBCDIC has a wider range of control characters than ASCII.

I try to create C/C++ code EBCDIC to Ascii Converter. Save this code with name main.c

/*
 * main.c
 *
 *  Created on: Mar 9, 2011
 *      Author: toto
 */
#include <stdlib.h>
#include <stdio.h>

unsigned char ebc_to_ascii_table(unsigned char ascii);
int main(int argc, char **argv)
{
    int n;
    int i;
    unsigned char asc;

    n = 4;
    unsigned char data[4] = {227, 136, 137, 162};
    for(i=0; i<n; i++)
    {
        asc = ebc_to_ascii_table(data[i]);
        printf("%c(%i)  -->  %c(%i) \n", data[i], data[i], asc, asc);
    }
    return (1);
}

unsigned char ebc_to_ascii_table(unsigned char  ascii)
{
    unsigned char asc;

    int Tableebc2Asc[256]=
    {
            32,  32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 91, 46, 60, 40, 43, 33,
            38, 32, 32, 32, 32, 32, 32, 32,
            32, 32, 93, 36, 42, 41, 59, 94,
            45, 47, 32, 32, 32, 32, 32, 32,
            32, 32,124, 44, 37, 95, 62, 63,
            32, 32, 32, 32, 32, 32,238,160,
            161, 96, 58, 35, 64, 39, 61, 34,
            230, 97, 98, 99,100,101,102,103,
            104,105,164,165,228,163,229,168,
            169,106,107,108,109,110,111,112,
            113,114,170,171,172,173,174,175,
            239,126,115,116,117,118,119,120,
            121,122,224,225,226,227,166,162,
            236,235,167,232,237,233,231,234,
            158,128,129,150,132,133,148,131,
            123, 65, 66, 67, 68, 69, 70, 71,
            72, 73,149,136,137,138,139,140,
            125, 74, 75, 76, 77, 78, 79, 80,
            81, 82,141,142,143,159,144,145,
            92, 32, 83, 84, 85, 86, 87, 88,
            89, 90,146,147,134,130,156,155,
            48, 49, 50, 51, 52, 53, 54, 55,
            56, 57,135,152,157,153,151, 32
    };

    asc=Tableebc2Asc[ascii];
    return (asc);
}

We can compile this code EBCDIC to Ascii Converter with command :

gcc main.c -o ebc2ascii

This is output program for EBCDIC to Ascii Converter :

�(227)  -->  T(84)
�(136)  -->  h(104)
�(137)  -->  i(105)
�(162)  -->  s(115)

You can read about how to convert Ascii to EBCDIC in here.

Source :

http://www.dynamoo.com/technical/ascii-ebcdic.htm
2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *