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
,