C/C++ : Ascii to EBCDIC Converter

Continue from my last post about Convert EBCDIC to Ascii Convert, now I try to create a program with C/C++ to convert Ascii to EBCDIC (ascii to ebcdic converter). Create a file main.c and copy this code :

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

unsigned char ascii_to_ebc_table(unsigned char ascii);
int main(int argc, char **argv)
{
    int n;
    int i;
    unsigned char ebc;

    n = 4;
    unsigned char data[4] = {84, 104, 105, 115};
    for(i=0; i<n; i++)
    {
        ebc = ascii_to_ebc_table(data[i]);
        printf("%c(%i)  -->  %c(%i) \n", data[i], data[i], ebc, ebc);
    }
    return (1);
}

unsigned char ascii_to_ebc_table(unsigned char ascii)
{
	unsigned char ebcd;
	static char tt[256]=
	{
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*         	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*         	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*         	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*         	*/
	0x40,0x4F,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,	/*  !"#$%&'	*/
	0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,	/* ()*+,-./	*/
	0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,	/* 01234567	*/
	0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,	/* 89:;<=>?	*/
	0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,	/* @ABCDEFG	*/
	0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,	/* HIJKLMNO	*/
	0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,	/* PQRSTUVW	*/
	0xE7,0xE8,0xE9,0x4A,0xE0,0x5A,0x5F,0x6D,	/* XYZ[\]^_	*/
	0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,	/* `abcdefg	*/
	0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,	/* hijklmno	*/
	0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,	/* pqrstuvw	*/
	0xA7,0xA8,0xA9,0xC0,0x6A,0xD0,0xA1,0x40,	/* xyz{|}~ 	*/
	0xB9,0xBA,0xED,0xBF,0xBC,0xBD,0xEC,0xFA,	/*              */
	0xCB,0xCC,0xCD,0xCE,0xCF,0xDA,0xDB,0xDC,	/*             	*/
	0xDE,0xDF,0xEA,0xEB,0xBE,0xCA,0xBB,0xFE,	/*		*/
	0xFB,0xFD,0x7d,0xEF,0xEE,0xFC,0xB8,0xDD,	/*		*/
	0x77,0x78,0xAF,0x8D,0x8A,0x8B,0xAE,0xB2,	/*		*/
	0x8F,0x90,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,	/*		*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*	       	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*	       	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*	       	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*	       	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*	       	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*	       	*/
	0xAA,0xAB,0xAC,0xAD,0x8C,0x8E,0x80,0xB6,	/* ����	*/
	0xB3,0xB5,0xB7,0xB1,0xB0,0xB4,0x76,0xA0,	/* 	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,	/*        	*/
	0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40 	/*        	*/
	};

	ebcd=tt[ascii];
	return (ebcd);
}

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

gcc main.c -o ascii2ebcdic

This is output program for Ascii to EBCDIC Converter :

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

You can read about convert ebcdic to ascii at here.

One Comment

Add a Comment

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.