EBCDIC、ASCII与EBCDIC的关系及相互转化

        EBCDIC (Extended Binary Coded Decimal Interchange Code) 为国际商用机器公司(IBM)于1963年-64年间推出的字符编码表,根据早期打孔机式的二进化十进数(BCD, Binary Coded Decimal)排列而成。

        EBCDIC编码中,英文字母不是连续地排列,中间出现多次断续,为撰写程序的人带来了一些困难。

        ASCII比EBCDIC后出现,ASCII的编码方式参照了EBCDIC,将英文连了起来,方便了程序员记忆。因此,就连IBM的个人计算机和工作站操作系统也不使用它们所有的EBCDIC编码。相反的,它们使用文本的工业标准编码,ASCII码。

        转化程序允许不同的操作系统从一种编码到另一种编码的转化。

(1)下面是ASCII和EBCDIC的编码转化表。

   The  following  table  shows  ASCII  and  EBCDIC character code values along with their decimal,
   octal, and hexadecimal equivalents. Abreviations, such as NUL and SOH are spelled out at the end
   of the table, in order of appearance.

   Table C-1. ASCII/EBCDIC Character Sets
        ASCII          EBCDIC       Character Code Values
   Control/Graphic Control/Graphic Decimal Octal Hexadecimal
         NUL             NUL          0     000      00
         SOH             SOH          1     001      01
         STX             STX          2     002      02
         ETX             ETX          3     003      03
         EOT             PF           4     004      04
         ENQ             HT           5     005      05
         ACK             LC           6     006      06
         BEL             DEL          7     007      07
         BS                           8     010      08
         HT                           9     011      09
         LF              SMM         10     012      0A
         VT              VT          11     013      0B
         FF              FF          12     014      0C
         CR              CR          13     015      0D
         SO              SO          14     016      0E
         SI              SI          15     017      0F
         DLE             DLE         16     020      10
         DC1             DC1         17     021      11
         DC2             DC2         18     022      12
         DC3             TM          19     023      13
         DC4             RES         20     024      14
         NAK             NL          21     025      15
         SYN             BS          22     026      16
         ETB             IL          23     027      17
         CAN             CAN         24     030      18
         EM              EM          25     031      19
         SUB             CC          26     032      1A
         ESC             CU1         27     033      1B
         FS              IFS         28     034      1C
         GS              IGS         29     035      1D
         RS              IRS         30     036      1E
         US              IUS         31     037      1F
         SP              DS          32     040      20
          !              SOS         33     041      21
          "              FS          34     042      22
          #                          35     043      23
          $              BYP         36     044      24
          %              LF          37     045      25
          &              ETB         38     046      26
          '              ESC         39     047      27
          (                          40     050      28
          )                          41     051      29
          *              SM          42     052      2A
          +              CU2         43     053      2B
          ,                          44     054      2C
          -              ENQ         45     055      2D
          .              ACK         46     056      2E
          /              BEL         47     057      2F
          0                          48     060      30
          1                          49     061      31
          2              SYN         50     062      32
          3                          51     063      33
          4              PN          52     064      34
          5              RS          53     065      35
          6              UC          54     066      36
          7              EOT         55     067      37
          8                          56     070      38
          9                          57     071      39
          :                          58     072      3A
          ;              CU3         59     073      3B
          <              DC4         60     074      3C
          =              NAK         61     075      3D
          >                          62     076      3E
          ?              SUB         63     077      3F
          @              SP          64     100      40
          A                          65     101      41
          B                          66     102      42
          C                          67     103      43
          D                          68     104      44
          E                          69     105      45
          F                          70     106      46
          G                          71     107      47
          H                          72     110      48
          I                          73     111      49
          J                          74     112      4A
          K               .          75     113      4B
          L               <          76     114      4C
          M               (          77     115      4D
          N               +          78     116      4E
          O               |          79     117      4F
          P               &          80     120      50
          Q                          81     121      51
          R                          82     122      52
          S                          83     123      53
          T                          84     124      54
          U                          85     125      55
          V                          86     126      56
          W                          87     127      57
          X                          88     130      58
          Y                          89     131      59
          Z               !          90     132      5A
          [               $          91     133      5B
          \               *          92     134      5C
          ]               )          93     135      5D
          ^               ;          94     136      5E
          _                          95     137      5F
          `               -          96     140      60
          a               /          97     141      61
          b                          98     142      62
          c                          99     143      63
          d                          100    144      64
          e                          101    145      65
          f                          102    146      66
          g                          103    147      67
          h                          104    150      68
          i                          105    151      69
          j                          106    152      6A
          k               ,          107    153      6B
          l               %          108    154      6C
          m               _          109    155      6D
          n               >          110    156      6E
          o               ?          111    157      6F
          p                          112    160      70
          q                          113    161      71
          r                          114    162      72
          s                          115    163      73
          t                          116    164      74
          u                          117    165      75
          v                          118    166      76
          w                          119    167      77
          x                          120    170      78
          y                          121    171      79
          z               :          122    172      7A
          {               #          123    173      7B
          |               @          124    174      7C
          }               '          125    175      7D
          ~               =          126    176      7E
         DEL              "          127    177      7F
                                     128    200      80
                          a          129    201      81
                          b          130    202      82
                          c          131    203      83
                          d          132    204      84
                          e          133    205      85
                          f          134    206      86
                          g          135    207      87
                          h          136    210      88
                          i          137    211      89
                                     138    212      8A
                                     139    213      8B
                                     140    214      8C
                                     141    215      8D
                                     142    216      8E
                                     143    217      8F
                          j          144    220      90
                          k          145    221      91
                          l          146    222      92
                                     147    223      93
                          m          148    224      94
                          n          149    225      95
                          o          150    226      96
                          p          151    227      97
                          q          152    230      98
                          r          153    231      99
                                     154    232      9A
                                     155    233      9B
                                     156    234      9C
                                     157    235      9D
                                     158    236      9E
                                     159    237      9F
                                     160    240      A0
                          ~          161    241      A1
                          s          162    242      A2
                          t          163    243      A3
                          u          164    244      A4
                          v          165    245      A5
                          w          166    246      A6
                          x          167    247      A7
                          y          168    250      A8
                          z          169    251      A9
                                     170    252      AA
                                     171    253      AB
                                     172    254      AC
                                     173    255      AD
                                     174    256      AE
                                     175    257      AF
                                     176    260      B0
                                     177    261      B1
                                     178    262      B2
                                     179    263      B3
                                     180    264      B4
                                     181    265      B5
                                     182    266      B6
                                     183    267      B7
                                     184    270      B8
                                     185    271      B9
                                     186    272      BA
                                     187    273      BB
                                     188    274      BC
                                     189    275      BD
                                     190    276      BE
                                     191    277      BF
                                     192    300      C0
                          A          193    301      C1
                          B          194    302      C2
                          C          195    303      C3
                          D          196    304      C4
                          E          197    305      C5
                          F          198    306      C6
                          G          199    307      C7
                          H          200    310      C8
                          I          201    311      C9
                                     202    312      CA
                                     203    313      CB
                                     204    314      CC
                                     205    315      CD
                                     206    316      CE
                                     207    317      CF
                                     208    320      D0
                          J          209    321      D1
                          K          210    322      D2
                          L          211    323      D3
                          M          212    324      D4
                          N          213    325      D5
                          O          214    326      D6
                          P          215    327      D7
                          Q          216    330      D8
                          R          217    331      D9
                                     218    332      DA
                                     219    333      DB
                                     220    334      DC
                                     221    335      DD
                                     222    336      DE
                                     223    337      DF
                          \          224    340      E0
                                     225    341      E1
                          S          226    342      E2
                          T          227    343      E3
                          U          228    344      E4
                          V          229    345      E5
                          W          230    346      E6
                          X          231    347      E7
                          Y          232    350      E8
                          Z          233    351      E9
                                     234    352      EA
                                     235    353      EB
                                     236    354      EC
                                     237    355      ED
                                     238    356      EE
                                     239    357      EF
                          0          240    360      F0
                          1          241    361      F1
                          2          242    362      F2
                          3          243    363      F3
                          4          244    364      F4
                          5          245    365      F5
                          6          246    366      F6
                          7          247    367      F7
                          8          248    370      F8
                          9          249    371      F9
                                     250    372      FA
                                     251    373      FB
                                     252    374      FC
                                     253    375      FD
                                     254    376      FE
                                     255    377      FF

   NUL   Null
   SOH   Start of Heading
   STX   Start of Text
   ETX   End of Text
   EOT   End of Transmission
   ENQ   Enquiry
   ACK   acknowledge
   BEL   Bell
   BS    Backspace
   HT    Horizontal Tabulation
   LF    Line Feed
   VT    Vertical Tabulation
   FF    Form Feed
   CR    Carriage Return
   SO    Shift Out
   SI    Shift In
   DLE   Data Link Escape
   DC1   Device Control 1 (X-ON)
   DC2   Device Control 2
   DC3   Device Control 3 (X-OFF)
   DC4   Device Control 4
   NAK   Negative Acknowledge
   SYN   Synchronous Idle
   ETB   End of Transmission Block
   CAN   Cancel
   EM    End of Medium
   SUB   Substitute
   ESC   Escape
   FS    File Separator
   GS    Group Separator
   RS    Record Separator
   US    Unit Separator
   SP    Space (Blank)
   DEL   Delete
(2)下面提供一个ASCII和EBCDIC转化的Java程序

public static final byte[] EBCDIC2ASCII = new byte[] {
	(byte)0x0,  (byte)0x1,  (byte)0x2,  (byte)0x3, 
	(byte)0x9C, (byte)0x9,  (byte)0x86, (byte)0x7F, 
	(byte)0x97, (byte)0x8D, (byte)0x8E, (byte)0xB, 
	(byte)0xC,  (byte)0xD,  (byte)0xE,  (byte)0xF, 
	(byte)0x10, (byte)0x11, (byte)0x12, (byte)0x13, 
	(byte)0x9D, (byte)0xA,  (byte)0x8,  (byte)0x87, 
	(byte)0x18, (byte)0x19, (byte)0x92, (byte)0x8F, 
	(byte)0x1C, (byte)0x1D, (byte)0x1E, (byte)0x1F, 
	(byte)0x80, (byte)0x81, (byte)0x82, (byte)0x83, 
	(byte)0x84, (byte)0x85, (byte)0x17, (byte)0x1B, 
	(byte)0x88, (byte)0x89, (byte)0x8A, (byte)0x8B, 
	(byte)0x8C, (byte)0x5,  (byte)0x6,  (byte)0x7, 
	(byte)0x90, (byte)0x91, (byte)0x16, (byte)0x93, 
	(byte)0x94, (byte)0x95, (byte)0x96, (byte)0x4, 
	(byte)0x98, (byte)0x99, (byte)0x9A, (byte)0x9B, 
	(byte)0x14, (byte)0x15, (byte)0x9E, (byte)0x1A, 
	(byte)0x20, (byte)0xA0, (byte)0xE2, (byte)0xE4, 
	(byte)0xE0, (byte)0xE1, (byte)0xE3, (byte)0xE5, 
	(byte)0xE7, (byte)0xF1, (byte)0xA2, (byte)0x2E, 
	(byte)0x3C, (byte)0x28, (byte)0x2B, (byte)0x7C, 
	(byte)0x26, (byte)0xE9, (byte)0xEA, (byte)0xEB, 
	(byte)0xE8, (byte)0xED, (byte)0xEE, (byte)0xEF, 
	(byte)0xEC, (byte)0xDF, (byte)0x21, (byte)0x24, 
	(byte)0x2A, (byte)0x29, (byte)0x3B, (byte)0x5E, 
	(byte)0x2D, (byte)0x2F, (byte)0xC2, (byte)0xC4, 
	(byte)0xC0, (byte)0xC1, (byte)0xC3, (byte)0xC5, 
	(byte)0xC7, (byte)0xD1, (byte)0xA6, (byte)0x2C, 
	(byte)0x25, (byte)0x5F, (byte)0x3E, (byte)0x3F, 
	(byte)0xF8, (byte)0xC9, (byte)0xCA, (byte)0xCB, 
	(byte)0xC8, (byte)0xCD, (byte)0xCE, (byte)0xCF, 
	(byte)0xCC, (byte)0x60, (byte)0x3A, (byte)0x23, 
	(byte)0x40, (byte)0x27, (byte)0x3D, (byte)0x22, 
	(byte)0xD8, (byte)0x61, (byte)0x62, (byte)0x63, 
	(byte)0x64, (byte)0x65, (byte)0x66, (byte)0x67, 
	(byte)0x68, (byte)0x69, (byte)0xAB, (byte)0xBB, 
	(byte)0xF0, (byte)0xFD, (byte)0xFE, (byte)0xB1, 
	(byte)0xB0, (byte)0x6A, (byte)0x6B, (byte)0x6C, 
	(byte)0x6D, (byte)0x6E, (byte)0x6F, (byte)0x70, 
	(byte)0x71, (byte)0x72, (byte)0xAA, (byte)0xBA, 
	(byte)0xE6, (byte)0xB8, (byte)0xC6, (byte)0xA4, 
	(byte)0xB5, (byte)0x7E, (byte)0x73, (byte)0x74, 
	(byte)0x75, (byte)0x76, (byte)0x77, (byte)0x78, 
	(byte)0x79, (byte)0x7A, (byte)0xA1, (byte)0xBF, 
	(byte)0xD0, (byte)0x5B, (byte)0xDE, (byte)0xAE, 
	(byte)0xAC, (byte)0xA3, (byte)0xA5, (byte)0xB7, 
	(byte)0xA9, (byte)0xA7, (byte)0xB6, (byte)0xBC, 
	(byte)0xBD, (byte)0xBE, (byte)0xDD, (byte)0xA8, 
	(byte)0xAF, (byte)0x5D, (byte)0xB4, (byte)0xD7, 
	(byte)0x7B, (byte)0x41, (byte)0x42, (byte)0x43, 
	(byte)0x44, (byte)0x45, (byte)0x46, (byte)0x47, 
	(byte)0x48, (byte)0x49, (byte)0xAD, (byte)0xF4, 
	(byte)0xF6, (byte)0xF2, (byte)0xF3, (byte)0xF5, 
	(byte)0x7D, (byte)0x4A, (byte)0x4B, (byte)0x4C, 
	(byte)0x4D, (byte)0x4E, (byte)0x4F, (byte)0x50, 
	(byte)0x51, (byte)0x52, (byte)0xB9, (byte)0xFB, 
	(byte)0xFC, (byte)0xF9, (byte)0xFA, (byte)0xFF, 
	(byte)0x5C, (byte)0xF7, (byte)0x53, (byte)0x54, 
	(byte)0x55, (byte)0x56, (byte)0x57, (byte)0x58, 
	(byte)0x59, (byte)0x5A, (byte)0xB2, (byte)0xD4, 
	(byte)0xD6, (byte)0xD2, (byte)0xD3, (byte)0xD5, 
	(byte)0x30, (byte)0x31, (byte)0x32, (byte)0x33, 
	(byte)0x34, (byte)0x35, (byte)0x36, (byte)0x37, 
	(byte)0x38, (byte)0x39, (byte)0xB3, (byte)0xDB, 
	(byte)0xDC, (byte)0xD9, (byte)0xDA, (byte)0x9F
};
public static final byte[] ASCII2EBCDIC = new byte[] {
	(byte)0x0,  (byte)0x1,  (byte)0x2,  (byte)0x3, 
	(byte)0x37, (byte)0x2D, (byte)0x2E, (byte)0x2F, 
	(byte)0x16, (byte)0x5,  (byte)0x15, (byte)0xB, 
	(byte)0xC,  (byte)0xD,  (byte)0xE,  (byte)0xF, 
	(byte)0x10, (byte)0x11, (byte)0x12, (byte)0x13, 
	(byte)0x3C, (byte)0x3D, (byte)0x32, (byte)0x26, 
	(byte)0x18, (byte)0x19, (byte)0x3F, (byte)0x27, 
	(byte)0x1C, (byte)0x1D, (byte)0x1E, (byte)0x1F, 
	(byte)0x40, (byte)0x5A, (byte)0x7F, (byte)0x7B, 
	(byte)0x5B, (byte)0x6C, (byte)0x50, (byte)0x7D, 
	(byte)0x4D, (byte)0x5D, (byte)0x5C, (byte)0x4E, 
	(byte)0x6B, (byte)0x60, (byte)0x4B, (byte)0x61, 
	(byte)0xF0, (byte)0xF1, (byte)0xF2, (byte)0xF3, 
	(byte)0xF4, (byte)0xF5, (byte)0xF6, (byte)0xF7, 
	(byte)0xF8, (byte)0xF9, (byte)0x7A, (byte)0x5E, 
	(byte)0x4C, (byte)0x7E, (byte)0x6E, (byte)0x6F, 
	(byte)0x7C, (byte)0xC1, (byte)0xC2, (byte)0xC3, 
	(byte)0xC4, (byte)0xC5, (byte)0xC6, (byte)0xC7, 
	(byte)0xC8, (byte)0xC9, (byte)0xD1, (byte)0xD2, 
	(byte)0xD3, (byte)0xD4, (byte)0xD5, (byte)0xD6, 
	(byte)0xD7, (byte)0xD8, (byte)0xD9, (byte)0xE2, 
	(byte)0xE3, (byte)0xE4, (byte)0xE5, (byte)0xE6, 
	(byte)0xE7, (byte)0xE8, (byte)0xE9, (byte)0xAD, 
	(byte)0xE0, (byte)0xBD, (byte)0x5F, (byte)0x6D, 
	(byte)0x79, (byte)0x81, (byte)0x82, (byte)0x83, 
	(byte)0x84, (byte)0x85, (byte)0x86, (byte)0x87, 
	(byte)0x88, (byte)0x89, (byte)0x91, (byte)0x92, 
	(byte)0x93, (byte)0x94, (byte)0x95, (byte)0x96, 
	(byte)0x97, (byte)0x98, (byte)0x99, (byte)0xA2, 
	(byte)0xA3, (byte)0xA4, (byte)0xA5, (byte)0xA6, 
	(byte)0xA7, (byte)0xA8, (byte)0xA9, (byte)0xC0, 
	(byte)0x4F, (byte)0xD0, (byte)0xA1, (byte)0x7, 
	(byte)0x20, (byte)0x21, (byte)0x22, (byte)0x23, 
	(byte)0x24, (byte)0x25, (byte)0x6,  (byte)0x17, 
	(byte)0x28, (byte)0x29, (byte)0x2A, (byte)0x2B, 
	(byte)0x2C, (byte)0x9,  (byte)0xA,  (byte)0x1B, 
	(byte)0x30, (byte)0x31, (byte)0x1A, (byte)0x33, 
	(byte)0x34, (byte)0x35, (byte)0x36, (byte)0x8, 
	(byte)0x38, (byte)0x39, (byte)0x3A, (byte)0x3B, 
	(byte)0x4,  (byte)0x14, (byte)0x3E, (byte)0xFF, 
	(byte)0x41, (byte)0xAA, (byte)0x4A, (byte)0xB1, 
	(byte)0x9F, (byte)0xB2, (byte)0x6A, (byte)0xB5, 
	(byte)0xBB, (byte)0xB4, (byte)0x9A, (byte)0x8A, 
	(byte)0xB0, (byte)0xCA, (byte)0xAF, (byte)0xBC, 
	(byte)0x90, (byte)0x8F, (byte)0xEA, (byte)0xFA, 
	(byte)0xBE, (byte)0xA0, (byte)0xB6, (byte)0xB3, 
	(byte)0x9D, (byte)0xDA, (byte)0x9B, (byte)0x8B, 
	(byte)0xB7, (byte)0xB8, (byte)0xB9, (byte)0xAB, 
	(byte)0x64, (byte)0x65, (byte)0x62, (byte)0x66, 
	(byte)0x63, (byte)0x67, (byte)0x9E, (byte)0x68, 
	(byte)0x74, (byte)0x71, (byte)0x72, (byte)0x73, 
	(byte)0x78, (byte)0x75, (byte)0x76, (byte)0x77, 
	(byte)0xAC, (byte)0x69, (byte)0xED, (byte)0xEE, 
	(byte)0xEB, (byte)0xEF, (byte)0xEC, (byte)0xBF, 
	(byte)0x80, (byte)0xFD, (byte)0xFE, (byte)0xFB, 
	(byte)0xFC, (byte)0xBA, (byte)0xAE, (byte)0x59, 
	(byte)0x44, (byte)0x45, (byte)0x42, (byte)0x46, 
	(byte)0x43, (byte)0x47, (byte)0x9C, (byte)0x48, 
	(byte)0x54, (byte)0x51, (byte)0x52, (byte)0x53, 
	(byte)0x58, (byte)0x55, (byte)0x56, (byte)0x57, 
	(byte)0x8C, (byte)0x49, (byte)0xCD, (byte)0xCE, 
	(byte)0xCB, (byte)0xCF, (byte)0xCC, (byte)0xE1, 
	(byte)0x70, (byte)0xDD, (byte)0xDE, (byte)0xDB, 
	(byte)0xDC, (byte)0x8D, (byte)0x8E, (byte)0xDF
};
public static byte[] ebcdicToAsciiBytes(byte[] e, int offset, int len) {
	byte[] a = new byte[len];
	for (int i=0; i

(3)在openssl里有个文件ebcdic.c,这个文件也实现了ASCII与IBM的EBCDIC编码的相互转化的方法。

/* crypto/ebcdic.c */

#ifndef CHARSET_EBCDIC

#include 
#if defined(PEDANTIC) || defined(__DECC) || defined(OPENSSL_SYS_MACOSX)
static void *dummy=&dummy;
#endif

#else /*CHARSET_EBCDIC*/

#include "ebcdic.h"
/*      Initial Port for  Apache-1.3     by 
 *      Adapted for       OpenSSL-0.9.4  by 
 */

#ifdef _OSD_POSIX
/*
    "BS2000 OSD" is a POSIX subsystem on a main frame.
    It is made by Siemens AG, Germany, for their BS2000 mainframe machines.
    Within the POSIX subsystem, the same character set was chosen as in
    "native BS2000", namely EBCDIC. (EDF04)

    The name "ASCII" in these routines is misleading: actually, conversion
    is not between EBCDIC and ASCII, but EBCDIC(EDF04) and ISO-8859.1;
    that means that (western european) national characters are preserved.

    This table is identical to the one used by rsh/rcp/ftp and other POSIX tools.
*/

/* Here's the bijective ebcdic-to-ascii table: */
const unsigned char os_toascii[256] = {
/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
       0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/
/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
       0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/
/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
       0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/
/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
       0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/
/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
       0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/
/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
       0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/
/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
       0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/
/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
       0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/
/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
       0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/
/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
       0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/
/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
       0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/
/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
       0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/
/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
       0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/
/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
       0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/
/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
       0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/
/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
       0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e  /*0123456789.{.}.~*/
};


/* The ascii-to-ebcdic table: */
const unsigned char os_toebcdic[256] = {
/*00*/  0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f,
	0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,  /*................*/
/*10*/  0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26,
	0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,  /*................*/
/*20*/  0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d,
	0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,  /* !"#$%&'()*+,-./ */
/*30*/  0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
	0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,  /*0123456789:;<=>?*/
/*40*/  0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
	0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,  /*@ABCDEFGHIJKLMNO*/
/*50*/  0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
	0xe7, 0xe8, 0xe9, 0xbb, 0xbc, 0xbd, 0x6a, 0x6d,  /*PQRSTUVWXYZ[\]^_*/
/*60*/  0x4a, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
	0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,  /*`abcdefghijklmno*/
/*70*/  0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
	0xa7, 0xa8, 0xa9, 0xfb, 0x4f, 0xfd, 0xff, 0x07,  /*pqrstuvwxyz{|}~.*/
/*80*/  0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08,
	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14,  /*................*/
/*90*/  0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17,
	0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0x5f,  /*................*/
/*a0*/  0x41, 0xaa, 0xb0, 0xb1, 0x9f, 0xb2, 0xd0, 0xb5,
	0x79, 0xb4, 0x9a, 0x8a, 0xba, 0xca, 0xaf, 0xa1,  /*................*/
/*b0*/  0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3,
	0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab,  /*................*/
/*c0*/  0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68,
	0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,  /*................*/
/*d0*/  0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf,
	0x80, 0xe0, 0xfe, 0xdd, 0xfc, 0xad, 0xae, 0x59,  /*................*/
/*e0*/  0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48,
	0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,  /*................*/
/*f0*/  0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1,
	0x70, 0xc0, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf   /*................*/
};

#else  /*_OSD_POSIX*/

/*
This code does basic character mapping for IBM's TPF and OS/390 operating systems.
It is a modified version of the BS2000 table.

Bijective EBCDIC (character set IBM-1047) to US-ASCII table:
This table is bijective - there are no ambigous or duplicate characters.
*/
const unsigned char os_toascii[256] = {
    0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, /* 00-0f:           */
    0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
    0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, /* 10-1f:           */
    0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
    0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, /* 20-2f:           */
    0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /* ................ */
    0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, /* 30-3f:           */
    0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /* ................ */
    0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, /* 40-4f:           */
    0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /*  ...........<(+| */
    0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, /* 50-5f:           */
    0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, /* &.........!$*);^ */
    0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, /* 60-6f:           */
    0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /* -/.........,%_>? */
    0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, /* 70-7f:           */
    0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /* .........`:#@'=" */
    0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 80-8f:           */
    0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /* .abcdefghi...... */
    0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, /* 90-9f:           */
    0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /* .jklmnopqr...... */
    0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, /* a0-af:           */
    0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, /* .~stuvwxyz...[.. */
    0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, /* b0-bf:           */
    0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, /* .............].. */
    0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* c0-cf:           */
    0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /* {ABCDEFGHI...... */
    0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* d0-df:           */
    0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, /* }JKLMNOPQR...... */
    0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, /* e0-ef:           */
    0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /* \.STUVWXYZ...... */
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* f0-ff:           */
    0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f  /* 0123456789...... */
};


/*
The US-ASCII to EBCDIC (character set IBM-1047) table:
This table is bijective (no ambiguous or duplicate characters)
*/
const unsigned char os_toebcdic[256] = {
    0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, /* 00-0f:           */
    0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
    0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, /* 10-1f:           */
    0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
    0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, /* 20-2f:           */
    0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, /*  !"#$%&'()*+,-./ */
    0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 30-3f:           */
    0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, /* 0123456789:;<=>? */
    0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 40-4f:           */
    0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, /* @ABCDEFGHIJKLMNO */
    0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, /* 50-5f:           */
    0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, /* PQRSTUVWXYZ[\]^_ */
    0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 60-6f:           */
    0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* `abcdefghijklmno */
    0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, /* 70-7f:           */
    0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, /* pqrstuvwxyz{|}~. */
    0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08, /* 80-8f:           */
    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14, /* ................ */
    0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17, /* 90-9f:           */
    0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0xff, /* ................ */
    0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, /* a0-af:           */
    0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, /* ................ */
    0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, /* b0-bf:           */
    0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, /* ................ */
    0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, /* c0-cf:           */
    0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, /* ................ */
    0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, /* d0-df:           */
    0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, /* ................ */
    0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, /* e0-ef:           */
    0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* ................ */
    0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, /* f0-ff:           */
    0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf  /* ................ */
};
#endif /*_OSD_POSIX*/

/* Translate a memory block from EBCDIC (host charset) to ASCII (net charset)
 * dest and srce may be identical, or separate memory blocks, but
 * should not overlap. These functions intentionally have an interface
 * compatible to memcpy(3).
 */

void *
ebcdic2ascii(void *dest, const void *srce, size_t count)
{
    unsigned char *udest = dest;
    const unsigned char *usrce = srce;

    while (count-- != 0) {
        *udest++ = os_toascii[*usrce++];
    }

    return dest;
}

void *
ascii2ebcdic(void *dest, const void *srce, size_t count)
{
    unsigned char *udest = dest;
    const unsigned char *usrce = srce;

    while (count-- != 0) {
        *udest++ = os_toebcdic[*usrce++];
    }

    return dest;
}

#endif


你可能感兴趣的:(编码)