ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다.  이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다.  ( 참고:  ASCII - Wikipedia, the free encyclopedia )

다음은  7bit ASCII 코드표를 만들어 보여주는 C 언어 소스 코드이다. 소스 코드 중에 진법변환에 필요한 함수

        convertAtoI()
        convertItoA()

의 구현도 포함되어 있다.



  1. /*
  2.  *  Filename: makeAsciiTable.c
  3.  *            Make a table of ascii codes.
  4.  *
  5.  *  Compile: cl makeAsciiTable.c
  6.  *  Execute: makeAsciiTable
  7.  *
  8.  *      Date:  2008/03/27
  9.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  10.  */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #define MAX_BUF 81
  15. enum { FALSE, TRUE };
  16. void println(char s[]) {
  17.     printf("%s\n", s);
  18. }
  19. void print(char s[]) {
  20.     printf("%s", s);
  21. }
  22. void printUsage() {
  23.     println("Usage: makeAsciiTable");
  24.     println("Make a table of ascii codes.");
  25. }
  26. char BASE36[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  27. void convertItoA(char *pstr, long num, int radix) {
  28.     char tmp[2];
  29.     char ret[MAX_BUF];
  30.     char arr[MAX_BUF];
  31.     long q, r;
  32.     int i, n;
  33.     int isNegative = FALSE;
  34.     if (num < 0L) {
  35.         isNegative = TRUE;
  36.         num = -num;
  37.     }
  38.     arr[0] = '\0';
  39.     q = num;
  40.     r = 0L;
  41.     while (q >= (long) radix) {
  42.         r = q % (long) radix;
  43.         q = q / (long) radix;
  44.         tmp[0] = BASE36[r];
  45.         tmp[1] = '\0';
  46.         strcat(arr, tmp);
  47.     }
  48.     tmp[0] = BASE36[q];
  49.     tmp[1] = '\0';
  50.     strcat(arr, tmp);
  51.     if (isNegative) {
  52.         strcat(arr, "-");
  53.     }
  54.     n = strlen(arr);
  55.     for (i = 0; i < n; i++) {
  56.         ret[i] = arr[n - i - 1];
  57.     }
  58.     ret[n] = '\0';
  59.     strcpy(pstr, (char *) ret);
  60. }
  61. long convertAtoI(const char *pstr, int radix) {
  62.     char tmp[2];
  63.     long ret = 0L;
  64.     char arr[MAX_BUF];
  65.     long q, r;
  66.     int isNegative = FALSE;
  67.     int len = strlen(pstr);
  68.     char c;
  69.     int i;
  70.     long val;
  71.     c = pstr[0];
  72.     if (c == '-') {
  73.        isNegative = TRUE;
  74.     }
  75.     else if (c >= '0' && c <= '9') {
  76.         ret = (long) (c - '0');
  77.     }
  78.     else if (c >= 'A' && c <= 'Z') {
  79.         ret = (long) (c - 'A') + 10L;
  80.     }
  81.     else if (c >= 'a' && c <= 'z') {
  82.         ret = (long) (c - 'a') + 10L;
  83.     }
  84.     if (ret >= (long) radix) {
  85.         println("        Invalid character!");
  86.         return ret;
  87.     }
  88.     for (i = 1; i < len; i++) {
  89.         c = pstr[i];
  90.         ret *= radix;
  91.         if (c >= '0' && c <= '9') {
  92.             val = (long) (c - '0');
  93.         }
  94.         else if (c >= 'A' && c <= 'Z') {
  95.             val = (long) (c - 'A') + 10L;
  96.         }
  97.         else if (c >= 'a' && c <= 'z') {
  98.             val = (long) (c - 'a') + 10L;
  99.         }
  100.         if (val >= (long) radix) {
  101.             println("        Invalid character!");
  102.             return ret;
  103.         }
  104.         ret += val;
  105.     }
  106.     return ret;
  107. }
  108. char asc[33][4]  = {
  109.      "NUL", "SOH", "STX", "ETX", "EOT",
  110.      "ENQ", "ACK", "BEL", "BS", "HT",
  111.      "LF", "VT", "FF", "CR", "SO",
  112.      "SI", "DLE", "DC1", "DC2", "DC3",
  113.      "DC4", "NAK", "SYN", "ETB", "CAN",
  114.      "EM", "SUB", "ESC", "FS", "GS",
  115.      "RS", "US", "Spc"
  116.  };
  117. char control[33][36]  = {
  118.         "NUL (null)",
  119.         "SOH (start of heading)",
  120.         "STX (start of text)",
  121.         "ETX (end of text)",
  122.         "EOT (end of transmission)",
  123.         "ENQ (enquiry)",
  124.         "ACK (acknowledge)",
  125.         "BEL (bell)",
  126.         "BS  (backspace)",
  127.         "TAB (horizontal tab)",
  128.         "LF  (line feed, NL new line)",
  129.         "VT  (vertical tab)",
  130.         "FF  (form feed, NP new page)",
  131.         "CR  (carriage return)",
  132.         "SO  (shift out)",
  133.         "SI  (shift in)",
  134.         "DLE (data link escape)",
  135.         "DC1 (device control 1)",
  136.         "DC2 (device control 2)",
  137.         "DC3 (device control 3)",
  138.         "DC4 (device control 4)",
  139.         "NAK (negative acknowledge)",
  140.         "SYN (synchronous idle)",
  141.         "ETB (end of trans. block)",
  142.         "CAN (cancel)",
  143.         "EM  (end of medium)",
  144.         "SUB (substitute, EOF end of file)",
  145.         "ESC (escape)",
  146.         "FS  (file separator)",
  147.         "GS  (group separator)",
  148.         "RS  (record separator)",
  149.         "US  (unit separator)",
  150.     };
  151. void makeTable() {
  152.     char sbuf[MAX_BUF];
  153.     char abuf[MAX_BUF/2];
  154.     char tbuf[MAX_BUF/2];
  155.     int i, j;
  156.     unsigned char c;
  157.     print("    ");
  158.     for (i = 0; i < 8; i++) {
  159.         print("+----");
  160.     }
  161.     print("+");
  162.     println("");
  163.     print("    ");
  164.     print("| 0- ");
  165.     print("| 1- ");
  166.     print("| 2- ");
  167.     print("| 3- ");
  168.     print("| 4- ");
  169.     print("| 5- ");
  170.     print("| 6- ");
  171.     print("| 7- ");
  172.     print("|");
  173.     println("");
  174.     print("+---");
  175.     for (i = 0; i < 8; i++) {
  176.         print("+----");
  177.     }
  178.     print("+");
  179.     println("");
  180.     for (i = 0; i < 16; i++) {
  181.         tbuf[0] = '\0';
  182.         convertItoA(sbuf, (long) i, 16);
  183.         strcat(tbuf, "| ");
  184.         strcat(tbuf, sbuf);
  185.         strcat(tbuf, " ");
  186.         for (j = 0; j < 8; j++) {
  187.             if (j*16 + i <= 32) {
  188.                 sprintf(abuf, "| %-3s", asc[j*16 + i]);
  189.             }
  190.             else if (j*16 + i == 127) {
  191.                 sprintf(abuf, "| %-3s", "DEL");
  192.             }
  193.             else {
  194.                 c = (char) (j*16 + i);
  195.                 sprintf(abuf, "| %2c ", c);
  196.             }
  197.             strcat(tbuf, abuf);
  198.         }
  199.         strcat(tbuf, "|");
  200.         println(tbuf);
  201.     }
  202.     print("+---");
  203.     for (i = 0; i < 8; i++) {
  204.         print("+----");
  205.     }
  206.     print("+");
  207.     println("");
  208.     println("");
  209.     for (i = 0; i < 16; i++) {
  210.         sprintf(sbuf, "%-30s",  control[i]);
  211.         sprintf(tbuf, "  %-34s",  control[i+16]);
  212.         print(sbuf);
  213.         println(tbuf);
  214.     }
  215. }
  216. void main(int argc, char *argv[]) {
  217.     if (argc > 1 && strcmp("-h", argv[1]) == 0) {
  218.         printUsage();
  219.         exit(1);
  220.     }
  221.     makeTable();
  222. }




컴파일> cl makeAsciiTable.c

실행> makeAsciiTable

    +----+----+----+----+----+----+----+----+
    | 0- | 1- | 2- | 3- | 4- | 5- | 6- | 7- |
+---+----+----+----+----+----+----+----+----+
| 0 | NUL| DLE| Spc|  0 |  @ |  P |  ` |  p |
| 1 | SOH| DC1|  ! |  1 |  A |  Q |  a |  q |
| 2 | STX| DC2|  " |  2 |  B |  R |  b |  r |
| 3 | ETX| DC3|  # |  3 |  C |  S |  c |  s |
| 4 | EOT| DC4|  $ |  4 |  D |  T |  d |  t |
| 5 | ENQ| NAK|  % |  5 |  E |  U |  e |  u |
| 6 | ACK| SYN|  & |  6 |  F |  V |  f |  v |
| 7 | BEL| ETB|  ' |  7 |  G |  W |  g |  w |
| 8 | BS | CAN|  ( |  8 |  H |  X |  h |  x |
| 9 | HT | EM |  ) |  9 |  I |  Y |  i |  y |
| A | LF | SUB|  * |  : |  J |  Z |  j |  z |
| B | VT | ESC|  + |  ; |  K |  [ |  k |  { |
| C | FF | FS |  , |  < |  L |  \ |  l |  | |
| D | CR | GS |  - |  = |  M |  ] |  m |  } |
| E | SO | RS |  . |  > |  N |  ^ |  n |  ~ |
| F | SI | US |  / |  ? |  O |  _ |  o | DEL|
+---+----+----+----+----+----+----+----+----+

NUL (null)                      DLE (data link escape)
SOH (start of heading)          DC1 (device control 1)
STX (start of text)             DC2 (device control 2)
ETX (end of text)               DC3 (device control 3)
EOT (end of transmission)       DC4 (device control 4)
ENQ (enquiry)                   NAK (negative acknowledge)
ACK (acknowledge)               SYN (synchronous idle)
BEL (bell)                      ETB (end of trans. block)
BS  (backspace)                 CAN (cancel)
TAB (horizontal tab)            EM  (end of medium)
LF  (line feed, NL new line)    SUB (substitute, EOF end of file)
VT  (vertical tab)              ESC (escape)
FF  (form feed, NP new page)    FS  (file separator)
CR  (carriage return)           GS  (group separator)
SO  (shift out)                 RS  (record separator)
SI  (shift in)                  US  (unit separator)


Posted by Scripter
,