MKoD - D Programming Language

EBCDIC to ASCII / ASCII to EBCDIC - code-name ebcdic.d

Very Kool! Convert EBCDIC to ASCII character mapping example:

/+
 ' Source   : ebcdic.d - converts a character between ASCII and EBCDIC mapping.
 ' Author   : The author of the original C code is unknown, but this source was  
 '          : found on a code sharing site on the Internet many years ago.
 ' Original : 05.Jul.97 - when C code was last modified.
 ' Created  : 22.Oct.04 - Ported to D, then "Compiled and Tested" with dmd v0.104
 ' Modified : (none)
 ' License  : Public Domain
 ' --------------------------------------------------------------
 ' Note: This code added to the site in case it might be useful to someone 
 '       who moves data between PCs and IBM main-frames.
 '
 ' To Compile: C:\dmd>bin\dmd ebcdic.d
 +/
private import std.stdio;

static char[ 256U ] a2e = 
[
       0U,   1U,   2U,   3U,  55U,  45U,  46U,  47U,  22U,   5U,  37U,  11U,  12U,  13U,  14U,  15U, 
      16U,  17U,  18U,  19U,  60U,  61U,  50U,  38U,  24U,  25U,  63U,  39U,  28U,  29U,  30U,  31U, 
      64U,  79U, 127U, 123U,  91U, 108U,  80U, 125U,  77U,  93U,  92U,  78U, 107U,  96U,  75U,  97U, 
     240U, 241U, 242U, 243U, 244U, 245U, 246U, 247U, 248U, 249U, 122U,  94U,  76U, 126U, 110U, 111U, 
     124U, 193U, 194U, 195U, 196U, 197U, 198U, 199U, 200U, 201U, 209U, 210U, 211U, 212U, 213U, 214U, 
     215U, 216U, 217U, 226U, 227U, 228U, 229U, 230U, 231U, 232U, 233U,  74U, 224U,  90U,  95U, 109U, 
     121U, 129U, 130U, 131U, 132U, 133U, 134U, 135U, 136U, 137U, 145U, 146U, 147U, 148U, 149U, 150U, 
     151U, 152U, 153U, 162U, 163U, 164U, 165U, 166U, 167U, 168U, 169U, 192U, 106U, 208U, 161U,   7U, 
      32U,  33U,  34U,  35U,  36U,  21U,   6U,  23U,  40U,  41U,  42U,  43U,  44U,   9U,  10U,  27U, 
      48U,  49U,  26U,  51U,  52U,  53U,  54U,   8U,  56U,  57U,  58U,  59U,   4U,  20U,  62U, 225U, 
      65U,  66U,  67U,  68U,  69U,  70U,  71U,  72U,  73U,  81U,  82U,  83U,  84U,  85U,  86U,  87U, 
      88U,  89U,  98U,  99U, 100U, 101U, 102U, 103U, 104U, 105U, 112U, 113U, 114U, 115U, 116U, 117U, 
     118U, 119U, 120U, 128U, 138U, 139U, 140U, 141U, 142U, 143U, 144U, 154U, 155U, 156U, 157U, 158U, 
     159U, 160U, 170U, 171U, 172U, 173U, 174U, 175U, 176U, 177U, 178U, 179U, 180U, 181U, 182U, 183U, 
     184U, 185U, 186U, 187U, 188U, 189U, 190U, 191U, 202U, 203U, 204U, 205U, 206U, 207U, 218U, 219U, 
     220U, 221U, 222U, 223U, 234U, 235U, 236U, 237U, 238U, 239U, 250U, 251U, 252U, 253U, 254U, 255U
];

static char[ 256U ] e2a = 
[
       0U,   1U,   2U,   3U, 156U,   9U, 134U, 127U, 151U, 141U, 142U,  11U,  12U,  13U,  14U,  15U, 
      16U,  17U,  18U,  19U, 157U, 133U,   8U, 135U,  24U,  25U, 146U, 143U,  28U,  29U,  30U,  31U, 
     128U, 129U, 130U, 131U, 132U,  10U,  23U,  27U, 136U, 137U, 138U, 139U, 140U,   5U,   6U,   7U, 
     144U, 145U,  22U, 147U, 148U, 149U, 150U,   4U, 152U, 153U, 154U, 155U,  20U,  21U, 158U,  26U, 
      32U, 160U, 161U, 162U, 163U, 164U, 165U, 166U, 167U, 168U,  91U,  46U,  60U,  40U,  43U,  33U, 
      38U, 169U, 170U, 171U, 172U, 173U, 174U, 175U, 176U, 177U,  93U,  36U,  42U,  41U,  59U,  94U, 
      45U,  47U, 178U, 179U, 180U, 181U, 182U, 183U, 184U, 185U, 124U,  44U,  37U,  95U,  62U,  63U, 
     186U, 187U, 188U, 189U, 190U, 191U, 192U, 193U, 194U,  96U,  58U,  35U,  64U,  39U,  61U,  34U, 
     195U,  97U,  98U,  99U, 100U, 101U, 102U, 103U, 104U, 105U, 196U, 197U, 198U, 199U, 200U, 201U, 
     202U, 106U, 107U, 108U, 109U, 110U, 111U, 112U, 113U, 114U, 203U, 204U, 205U, 206U, 207U, 208U, 
     209U, 126U, 115U, 116U, 117U, 118U, 119U, 120U, 121U, 122U, 210U, 211U, 212U, 213U, 214U, 215U, 
     216U, 217U, 218U, 219U, 220U, 221U, 222U, 223U, 224U, 225U, 226U, 227U, 228U, 229U, 230U, 231U, 
     123U,  65U,  66U,  67U,  68U,  69U,  70U,  71U,  72U,  73U, 232U, 233U, 234U, 235U, 236U, 237U, 
     125U,  74U,  75U,  76U,  77U,  78U,  79U,  80U,  81U,  82U, 238U, 239U, 240U, 241U, 242U, 243U, 
      92U, 159U,  83U,  84U,  85U,  86U,  87U,  88U,  89U,  90U, 244U, 245U, 246U, 247U, 248U, 249U, 
      48U,  49U,  50U,  51U,  52U,  53U,  54U,  55U,  56U,  57U, 250U, 251U, 252U, 253U, 254U, 255U
];

dchar convertASCIItoEBCDIC( in dchar dc )
{
    uint uiPos = 0;
    	
    if ( dc > char.max ) return 0;
    uiPos = cast(uint)dc;
    
    return a2e[ uiPos ];
}

dchar convertEBCDICtoASCII( in dchar dc )
{
    uint uiPos = 0;
    
    if ( dc > char.max ) return 0;
    uiPos = cast(uint)dc;
    
    return e2a[ uiPos ];
}

int main()
{
    dchar[] sAsciiMap = "IBM Mainframe";
    dchar[] sEbcdicMap;
	
    writefln( "Convert ASCII to EBCDIC character mapping." );
    for ( int ibm = 0; ibm < sAsciiMap.length; ibm++ )
    {
        sEbcdicMap ~= convertASCIItoEBCDIC( sAsciiMap[ ibm ] );
        writefln( "ascii=0x%02X to ebcdic=0x%02X", cast(int)sAsciiMap[ ibm ], cast(int)sEbcdicMap[ ibm ] );
    }
 
    writefln();
    
    writefln( "Convert EBCDIC to ASCII character mapping." );
    for ( int ibm = 0; ibm < sEbcdicMap.length; ibm++ )
    {
        writefln( "ebcdic=0x%02X to ascii=0x%02X", cast(int)sEbcdicMap[ ibm ], cast(int)convertEBCDICtoASCII( sEbcdicMap[ ibm ] ) );
    }   
    
    return 0;
    
} // end int main()
C:\dmd>bin\dmd ebcdic.d
C:\dmd\bin\..\..\dm\bin\link.exe ebcdic,,,user32+kernel32/noi;

C:\dmd>ebcdic
Convert ASCII to EBCDIC character mapping.
ascii=0x49 to ebcdic=0xC9
ascii=0x42 to ebcdic=0xC2
ascii=0x4D to ebcdic=0xD4
ascii=0x20 to ebcdic=0x40
ascii=0x4D to ebcdic=0xD4
ascii=0x61 to ebcdic=0x81
ascii=0x69 to ebcdic=0x89
ascii=0x6E to ebcdic=0x95
ascii=0x66 to ebcdic=0x86
ascii=0x72 to ebcdic=0x99
ascii=0x61 to ebcdic=0x81
ascii=0x6D to ebcdic=0x94
ascii=0x65 to ebcdic=0x85

Convert EBCDIC to ASCII character mapping.
ebcdic=0xC9 to ascii=0x49
ebcdic=0xC2 to ascii=0x42
ebcdic=0xD4 to ascii=0x4D
ebcdic=0x40 to ascii=0x20
ebcdic=0xD4 to ascii=0x4D
ebcdic=0x81 to ascii=0x61
ebcdic=0x89 to ascii=0x69
ebcdic=0x95 to ascii=0x6E
ebcdic=0x86 to ascii=0x66
ebcdic=0x99 to ascii=0x72
ebcdic=0x81 to ascii=0x61
ebcdic=0x94 to ascii=0x6D
ebcdic=0x85 to ascii=0x65

C:\dmd>
Mars: fourth Rock from the Sun.