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

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

        convertAtoI()
        convertItoA()

의 구현도 포함되어 있다.



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




컴파일> cl /GX makeAsciiTableCPP.cpp

실행> makeAsciiTableCPP

    +----+----+----+----+----+----+----+----+
    | 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)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

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
,

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

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

        convertAtoI(string, radix)
        convertItoA(number, radix)

의 구현도 포함되어 있다.



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




실행> lua makeAsciiTable.lua

    +----+----+----+----+----+----+----+----+
    | 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)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

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

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

        convertAtoI(string, radix)
        convertItoA(number, radix)

의 구현도 포함되어 있다.



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




실행> ruby makeAsciiTable.rb

    +----+----+----+----+----+----+----+----+
    | 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)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

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

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

        convertAtoI(string, radix)
        convertItoA(number, radix)

의 구현도 포함되어 있다.

(아래의 소스는 Jython이나 IronPython에서도 수정없이 그대로 실행된다.)



  1. #  Filename: makeAsciiTable.py
  2. #            Make a table of ascii codes.
  3. #
  4. #  Execute: python makeAsciiTable.py
  5. #
  6. #      Date:  2008/03/28
  7. #    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  8. import sys
  9. BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  10. def println(s=None):
  11.     if s == None:
  12.         print
  13.     else:
  14.         print(s)
  15. def printUsage():
  16.     println("Usage: python makeAsciiTable.py")
  17.     println("Make a table of ascii codes.")
  18. def convertItoA(num, radix):
  19.     isNegative = False
  20.     if num < 0:
  21.         isNegative = True
  22.         num = -num
  23.     arr = ""
  24.     q = num
  25.     r = 0
  26.     while q >= radix:
  27.         r = q % radix
  28.         q = q / radix
  29.         arr += BASE36[r]
  30.     arr += BASE36[q]
  31.     if isNegative:
  32.         arr += "-"
  33.     n = len(arr)
  34.     ret = ""
  35.     for i in range(0, n):
  36.         ret += arr[n - i - 1]
  37.     return ret
  38. def convertAtoI(srcStr, radix):
  39.     isNegative = False
  40.     ret = 0
  41.     m = len(srcStr)
  42.     val = 0
  43.     c = srcStr[0]
  44.     if c == '-':
  45.         isNegative = True
  46.     elif c >= '0' and c <= '9':
  47.         ret = ord(c) - ord('0')
  48.     elif c >= 'A' and c <= 'Z':
  49.         ret = (ord(c) - ord('A')) + 10
  50.     elif c >= 'a' and c <= 'z':
  51.         ret = (ord(c) - ord('a')) + 10
  52.     if ret >= radix:
  53.         println("        Invalid character!")
  54.         return ret
  55.     for i in range(1, m):
  56.         c = srcStr[i]
  57.         ret *= radix
  58.         if c >= '0' and c <= '9':
  59.             val = ord(c) - ord('0')
  60.         elif c >= 'A' and c <= 'Z':
  61.             val = (ord(c) - ord('A')) + 10
  62.         elif c >= 'a' and c <= 'z':
  63.             val = (ord(c) - ord('a')) + 10
  64.         if val >= radix:
  65.             println("        Invalid character!")
  66.             return ret
  67.         ret += val
  68.     return ret
  69. asc = [
  70.     "NUL", "SOH", "STX", "ETX", "EOT",
  71.     "ENQ", "ACK", "BEL", "BS", "HT",
  72.     "LF", "VT", "FF", "CR", "SO",
  73.     "SI", "DLE", "DC1", "DC2", "DC3",
  74.     "DC4", "NAK", "SYN", "ETB", "CAN",
  75.     "EM", "SUB", "ESC", "FS", "GS",
  76.     "RS", "US", "Spc"
  77. ]
  78. control  = [
  79.     "NUL (null)",
  80.     "SOH (start of heading)",
  81.     "STX (start of text)",
  82.     "ETX (end of text)",
  83.     "EOT (end of transmission)",
  84.     "ENQ (enquiry)",
  85.     "ACK (acknowledge)",
  86.     "BEL (bell)",
  87.     "BS  (backspace)",
  88.     "TAB (horizontal tab)",
  89.     "LF  (line feed, NL new line)",
  90.     "VT  (vertical tab)",
  91.     "FF  (form feed, NP new page)",
  92.     "CR  (carriage return)",
  93.     "SO  (shift out)",
  94.     "SI  (shift in)",
  95.     "DLE (data link escape)",
  96.     "DC1 (device control 1)",
  97.     "DC2 (device control 2)",
  98.     "DC3 (device control 3)",
  99.     "DC4 (device control 4)",
  100.     "NAK (negative acknowledge)",
  101.     "SYN (synchronous idle)",
  102.     "ETB (end of trans. block)",
  103.     "CAN (cancel)",
  104.     "EM  (end of medium)",
  105.     "SUB (substitute, EOF end of file)",
  106.     "ESC (escape)",
  107.     "FS  (file separator)",
  108.     "GS  (group separator)",
  109.     "RS  (record separator)",
  110.     "US  (unit separator)",
  111. ]
  112. def makeTable():
  113.     sbuf = ""
  114.     abuf = ""
  115.     tbuf = ""
  116.     sbuf = "    "
  117.     for i in range(0, 8):
  118.         sbuf += "+----"
  119.     sbuf += "+"
  120.     println(sbuf)
  121.     sbuf = "    "
  122.     sbuf += "| 0- "
  123.     sbuf += "| 1- "
  124.     sbuf += "| 2- "
  125.     sbuf += "| 3- "
  126.     sbuf += "| 4- "
  127.     sbuf += "| 5- "
  128.     sbuf += "| 6- "
  129.     sbuf += "| 7- "
  130.     sbuf += "|"
  131.     println(sbuf)
  132.     sbuf = "+---"
  133.     for i in range(0, 8):
  134.         sbuf += "+----"
  135.     sbuf += "+"
  136.     println(sbuf)
  137.     for i in range(0, 16):
  138.         tbuf = ""
  139.         sbuf = convertItoA(i, 16)
  140.         tbuf += "| " + sbuf + " "
  141.         for j in range(0, 8):
  142.             if j*16 + i <= 32:
  143.                 abuf = "| %-3s" % asc[j*16 + i]
  144.             elif j*16 + i == 127:
  145.                 abuf = "| %-3s" % "DEL"
  146.             else:
  147.                 c = chr(j*16 + i)
  148.                 abuf = "| %2c " % c
  149.             tbuf += abuf
  150.         tbuf += "|"
  151.         println(tbuf)
  152.     sbuf = "+---"
  153.     for i in range(0, 8):
  154.         sbuf += "+----"
  155.     sbuf += "+"
  156.     println(sbuf)
  157.     println("")
  158.     for i in range(0, 16):
  159.         tbuf = "%-30s  %-34s" % (control[i], control[i+16])
  160.         println(tbuf)
  161. if len(sys.argv) > 1 and "-h" == sys.argv[1]:
  162.     printUsage()
  163.     sys.exit(1)
  164. makeTable()




실행> python makeAsciiTable.py

   
    +----+----+----+----+----+----+----+----+
    | 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)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

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

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

        convertAtoI(String, radix)
        convertItoA(long, radix)

의 구현도 포함되어 있다.


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




실행> groovy makeAsciiTable.groovy

    +----+----+----+----+----+----+----+----+
    | 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)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

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

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

        convertAtoI(String, radix)
        convertItoA(long, radix)

의 구현도 포함되어 있다.


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




컴파일> javac -d . MakAsciiTableApp.java

실행> java MakeAsciiTableApp

    +----+----+----+----+----+----+----+----+
    | 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)



Creative Commons License

이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은  0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 C++ 소스 코드이다. 진법 변환에 필요한 함수

        convertAtoI(string, radix)
        convertItoA(long, radix)

를 C++ 코드로 자체 작성하여 사용하였다.




  1. /*
  2.  *  Filename: makeRadixTableCPP.cpp
  3.  *            Show the radix table with 10-, 2-, 8-, 16-radices.
  4.  *
  5.  *  Compile: cl /EHsc makeRadixTableCPP.cpp
  6.  *  Execute: makeRadixTable
  7.  *
  8.  *      Date:  2008/03/27
  9.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  10.  */
  11. #include <iostream>
  12. #include <string>
  13. #include <cmath>
  14. using namespace std;
  15. #define MAX_BUF 81
  16. enum { FALSE, TRUE };
  17. void println(char s[]) {
  18.     cout << s << endl;
  19. }
  20. void print(char s[]) {
  21.     cout << s;
  22. }
  23. void printUsage() {
  24.     println("Usage: makeRadixTableCPP");
  25.     println("Show the radix table with 10-, 2-, 8-, 16-radices.");
  26. }
  27. char BASE36[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  28. void convertItoA(char *pstr, long num, int radix) {
  29.     char tmp[2];
  30.     char ret[MAX_BUF];
  31.     char arr[MAX_BUF];
  32.     long q, r;
  33.     int i, n;
  34.     int isNegative = FALSE;
  35.     if (num < 0L) {
  36.         isNegative = TRUE;
  37.         num = -num;
  38.     }
  39.     arr[0] = '\0';
  40.     q = num;
  41.     r = 0L;
  42.     while (q >= (long) radix) {
  43.         r = q % (long) radix;
  44.         q = q / (long) radix;
  45.         tmp[0] = BASE36[r];
  46.         tmp[1] = '\0';
  47.         strcat(arr, tmp);
  48.     }
  49.     tmp[0] = BASE36[q];
  50.     tmp[1] = '\0';
  51.     strcat(arr, tmp);
  52.     if (isNegative) {
  53.         strcat(arr, "-");
  54.     }
  55.     n = strlen(arr);
  56.     for (i = 0; i < n; i++) {
  57.         ret[i] = arr[n - i - 1];
  58.     }
  59.     ret[n] = '\0';
  60.     strcpy(pstr, (char *) ret);
  61. }
  62. long convertAtoI(const char *pstr, int radix) {
  63.     char tmp[2];
  64.     long ret = 0L;
  65.     char arr[MAX_BUF];
  66.     long q, r;
  67.     int isNegative = FALSE;
  68.     int len = strlen(pstr);
  69.     char c;
  70.     int i;
  71.     long val;
  72.     c = pstr[0];
  73.     if (c == '-') {
  74.        isNegative = TRUE;
  75.     }
  76.     else if (c >= '0' && c <= '9') {
  77.         ret = (long) (c - '0');
  78.     }
  79.     else if (c >= 'A' && c <= 'Z') {
  80.         ret = (long) (c - 'A') + 10L;
  81.     }
  82.     else if (c >= 'a' && c <= 'z') {
  83.         ret = (long) (c - 'a') + 10L;
  84.     }
  85.     if (ret >= (long) radix) {
  86.         println("        Invalid character!");
  87.         return ret;
  88.     }
  89.     for (i = 1; i < len; i++) {
  90.         c = pstr[i];
  91.         ret *= radix;
  92.         if (c >= '0' && c <= '9') {
  93.             val = (long) (c - '0');
  94.         }
  95.         else if (c >= 'A' && c <= 'Z') {
  96.             val = (long) (c - 'A') + 10L;
  97.         }
  98.         else if (c >= 'a' && c <= 'z') {
  99.             val = (long) (c - 'a') + 10L;
  100.         }
  101.         if (val >= (long) radix) {
  102.             println("        Invalid character!");
  103.             return ret;
  104.         }
  105.         ret += val;
  106.     }
  107.     return ret;
  108. }
  109. void makeTable() {
  110.     char sbuf[MAX_BUF];
  111.     char abuf[MAX_BUF/2];
  112.     char tbuf[MAX_BUF/2];
  113.     int i;
  114.     for (i = 0; i < 4; i++) {
  115.         print("+-------");
  116.     }
  117.     print("+");
  118.     println("");
  119.     print("|  Dec");
  120.     print("\t|   Bin");
  121.     print("\t|  Oct");
  122.     print("\t|  Hex  |");
  123.     println("");
  124.     for (i = 0; i < 4; i++) {
  125.         print("+-------");
  126.     }
  127.     print("+");
  128.     println("");
  129.     for (i = 0; i < 16; i++) {
  130.         sprintf(sbuf, "|   %2d", i);
  131.         convertItoA((char *)abuf, (long) i, 2);
  132.         sprintf(tbuf, "\t|  %4s", abuf);
  133.         strcat(sbuf, tbuf);
  134.         convertItoA((char *)abuf, (long) i, 8);
  135.         sprintf(tbuf, "\t|   %2s", abuf);
  136.         strcat(sbuf, tbuf);
  137.         convertItoA((char *)abuf, (long) i, 16);
  138.         sprintf(tbuf, "\t|    %-2s |", abuf);
  139.         strcat(sbuf, tbuf);
  140.         println(sbuf);
  141.     }
  142.     for (i = 0; i < 4; i++) {
  143.         print("+-------");
  144.     }
  145.     print("+");
  146.     println("");
  147. }
  148. void main(int argc, char *argv[]) {
  149.     if (argc > 1 && strcmp("-h", argv[1]) == 0) {
  150.         printUsage();
  151.         exit(1);
  152.     }
  153.     makeTable();
  154. }






컴파일> cl /EHsc makeRadixTableCPP.cpp

실행> makeRadixTableCPP

+-------+-------+-------+-------+
|  Dec  |   Bin |  Oct  |  Hex  |
+-------+-------+-------+-------+
|    0  |     0 |    0  |    0  |
|    1  |     1 |    1  |    1  |
|    2  |    10 |    2  |    2  |
|    3  |    11 |    3  |    3  |
|    4  |   100 |    4  |    4  |
|    5  |   101 |    5  |    5  |
|    6  |   110 |    6  |    6  |
|    7  |   111 |    7  |    7  |
|    8  |  1000 |   10  |    8  |
|    9  |  1001 |   11  |    9  |
|   10  |  1010 |   12  |    A  |
|   11  |  1011 |   13  |    B  |
|   12  |  1100 |   14  |    C  |
|   13  |  1101 |   15  |    D  |
|   14  |  1110 |   16  |    E  |
|   15  |  1111 |   17  |    F  |
+-------+-------+-------+-------+





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은  0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 C 소스 코드이다. 진법 변환에 필요한 함수

        convertAtoI(string, radix)
        convertItoA(long, radix)

를 C 코드로 자체 작성하여 사용하였다.
(Ch를 사용하면 컴파일 과정 없이 소스 코드를 그대로 실행시킬 수 있다.)



  1. /*
  2.  *  Filename: makeRadixTable.c
  3.  *            Show the radix table with 10-, 2-, 8-, 16-radices.
  4.  *
  5.  *  Compile: cl makeRadixTable.c
  6.  *  Execute: makeRadixTable
  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: makeRadixTable");
  24.     println("Show the radix table with 10-, 2-, 8-, 16-radices.");
  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. void makeTable() {
  109.     char sbuf[MAX_BUF];
  110.     char abuf[MAX_BUF/2];
  111.     char tbuf[MAX_BUF/2];
  112.     int i;
  113.     for (i = 0; i < 4; i++) {
  114.         print("+-------");
  115.     }
  116.     print("+");
  117.     println("");
  118.     print("|  Dec");
  119.     print("\t|   Bin");
  120.     print("\t|  Oct");
  121.     print("\t|  Hex  |");
  122.     println("");
  123.     for (i = 0; i < 4; i++) {
  124.         print("+-------");
  125.     }
  126.     print("+");
  127.     println("");
  128.     for (i = 0; i < 16; i++) {
  129.         sprintf(sbuf, "|   %2d", i);
  130.         convertItoA((char *)abuf, (long) i, 2);
  131.         sprintf(tbuf, "\t|  %4s", abuf);
  132.         strcat(sbuf, tbuf);
  133.         convertItoA((char *)abuf, (long) i, 8);
  134.         sprintf(tbuf, "\t|   %2s", abuf);
  135.         strcat(sbuf, tbuf);
  136.         convertItoA((char *)abuf, (long) i, 16);
  137.         sprintf(tbuf, "\t|    %-2s |", abuf);
  138.         strcat(sbuf, tbuf);
  139.         println(sbuf);
  140.     }
  141.     for (i = 0; i < 4; i++) {
  142.         print("+-------");
  143.     }
  144.     print("+");
  145.     println("");
  146. }
  147. void main(int argc, char *argv[]) {
  148.     if (argc > 1 && strcmp("-h", argv[1]) == 0) {
  149.         printUsage();
  150.         exit(1);
  151.     }
  152.     makeTable();
  153. }



컴파일> cl makeRadixTable.c

실행> makeRadixTable

+-------+-------+-------+-------+
|  Dec  |   Bin |  Oct  |  Hex  |
+-------+-------+-------+-------+
|    0  |     0 |    0  |    0  |
|    1  |     1 |    1  |    1  |
|    2  |    10 |    2  |    2  |
|    3  |    11 |    3  |    3  |
|    4  |   100 |    4  |    4  |
|    5  |   101 |    5  |    5  |
|    6  |   110 |    6  |    6  |
|    7  |   111 |    7  |    7  |
|    8  |  1000 |   10  |    8  |
|    9  |  1001 |   11  |    9  |
|   10  |  1010 |   12  |    A  |
|   11  |  1011 |   13  |    B  |
|   12  |  1100 |   14  |    C  |
|   13  |  1101 |   15  |    D  |
|   14  |  1110 |   16  |    E  |
|   15  |  1111 |   17  |    F  |
+-------+-------+-------+-------+



 

Posted by Scripter
,

컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은  0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 Lua 소스 코드이다. 진법 변환에 필요한 함수

        convertAtoI(string, radix)
        convertItoA(number, radix)

를 Lua 코드로 자체 작성하여 사용하였다.



  1. --  Filename: makeRadixTable.lua
  2. --            Show the radix table with 10-, 2-, 8-, 16-radices.
  3. --
  4. --  Execute: lua makeRadixTable.lua
  5. --
  6. --      Date:  2008/03/28
  7. --    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  8. BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. function println(s)
  10.     print(s)
  11. end
  12. function printUsage()
  13.     println("Usage: lua makeRadixTable.lua")
  14.     println("Show the radix table with 10-, 2-, 8-, 16-radices.")
  15. end
  16. function convertItoA(num, radix)
  17.     local isNegative = false
  18.     if num < 0 then
  19.         isNegative = true
  20.         num = -num
  21.     end
  22.     local arr = ""
  23.     local q = num
  24.     local r = 0
  25.     while q >= radix do
  26.         r = math.fmod(q, radix)
  27.         q = math.floor(q / radix)
  28.         arr = arr .. string.sub(BASE36, r+1, r+1)
  29.     end
  30.     arr = arr .. string.sub(BASE36, q+1, q+1)
  31.     if isNegative then
  32.         arr = arr .. "-"
  33.     end
  34.     local n = string.len(arr)
  35.     local ret = ""
  36.     for i = 1, n do
  37.         ret = ret .. string.sub(arr, n - i + 1, n - i + 1)
  38.     end
  39.     return ret
  40. end
  41. function convertAtoI(srcStr, radix)
  42.     local isNegative = false
  43.     local ret = 0
  44.     local len = string.len(srcStr)
  45.     local val = 0
  46.     local c
  47.     c = string.sub(srcStr, 1, 1)
  48.     if c == '-' then
  49.         isNegative = true
  50.     elseif c >= '0' and c <= '9' then
  51.         ret = string.byte(c) - string.byte('0')
  52.     elseif c >= 'A' and c <= 'Z' then
  53.         ret = string.byte(c) - string.byte('A') + 10
  54.     elseif c >= 'a' and c <= 'z' then
  55.         ret = string.byte(c) - string.byte('a') + 10
  56.     end
  57.     if ret >= radix then
  58.         println("        Invalid character!")
  59.         return ret
  60.     end
  61.     for i = 2, len do
  62.         c = string.sub(srcStr, i, i)
  63.         ret = ret * radix
  64.         if c >= '0' and c <= '9' then
  65.             val = string.byte(c) - string.byte('0')
  66.         elseif c >= 'A' and c <= 'Z' then
  67.             val = string.byte(c) - string.byte('A') + 10
  68.         elseif c >= 'a' and c <= 'z' then
  69.             val = string.byte(c) - string.byte('a') + 10
  70.         end
  71.         if val >= radix then
  72.             println("        Invalid character!")
  73.             return ret
  74.         end
  75.         ret = ret + val
  76.     end
  77.     return ret
  78. end
  79. function makeTable()
  80.     sbuf = ""
  81.     abuf = ""
  82.     tbuf = ""
  83.     for i = 1, 4 do
  84.         sbuf = sbuf .. "+-------"
  85.     end
  86.     sbuf = sbuf .. "+"
  87.     println(sbuf)
  88.     sbuf = "|  Dec"
  89.     sbuf = sbuf .. "\t|   Bin"
  90.     sbuf = sbuf .. "\t|  Oct"
  91.     sbuf = sbuf .. "\t|  Hex  |"
  92.     println(sbuf)
  93.     sbuf = ""
  94.     for i = 1, 4 do
  95.         sbuf = sbuf .. "+-------"
  96.     end
  97.     sbuf = sbuf .. "+"
  98.     println(sbuf)
  99.     for i = 0, 15 do
  100.         sbuf = string.format("|   %2d", i)
  101.         abuf = convertItoA(i, 2)
  102.         tbuf = string.format("\t|  %4s", abuf)
  103.         sbuf = sbuf .. tbuf
  104.         abuf = convertItoA(i, 8)
  105.         tbuf = string.format("\t|   %2s", abuf)
  106.         sbuf = sbuf .. tbuf
  107.         abuf = convertItoA(i, 16)
  108.         tbuf = string.format("\t|    %-2s |", abuf)
  109.         sbuf = sbuf .. tbuf
  110.         println(sbuf)
  111.     end
  112.     sbuf = ""
  113.     for i = 1, 4 do
  114.         sbuf = sbuf .. "+-------"
  115.     end
  116.     sbuf = sbuf .. "+"
  117.     println(sbuf)
  118. end
  119. if #arg > 0 and "-h" == arg[1] then
  120.     printUsage()
  121.     os.exit(1)
  122. end
  123. makeTable()



실행> lua makeRadixTable.lua

+-------+-------+-------+-------+
|  Dec  |   Bin |  Oct  |  Hex  |
+-------+-------+-------+-------+
|    0  |     0 |    0  |    0  |
|    1  |     1 |    1  |    1  |
|    2  |    10 |    2  |    2  |
|    3  |    11 |    3  |    3  |
|    4  |   100 |    4  |    4  |
|    5  |   101 |    5  |    5  |
|    6  |   110 |    6  |    6  |
|    7  |   111 |    7  |    7  |
|    8  |  1000 |   10  |    8  |
|    9  |  1001 |   11  |    9  |
|   10  |  1010 |   12  |    A  |
|   11  |  1011 |   13  |    B  |
|   12  |  1100 |   14  |    C  |
|   13  |  1101 |   15  |    D  |
|   14  |  1110 |   16  |    E  |
|   15  |  1111 |   17  |    F  |
+-------+-------+-------+-------+




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,