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
,