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

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

                atoi(string, radix)
                itoa(number, radix)

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

(아래의 소스는 Python 소스를 F# 소스로 일대일 변환 수정한 것이라서, F# 언어의 명령형 언어 특징을 위주로 작성되어 있다.)

  1. (*
  2.  *  Filename: MakeAsciiTable.fs
  3.  *            Make a table of ascii codes.
  4.  *
  5.  *  Compile: fsc MakeAsciiTable.fs
  6.  *  Execute: MakeAsciiTable
  7.  *
  8.  *      Date:  20010/07/15
  9.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  10.  *)
  11. #light
  12. exception RuntimeError of string
  13. exception ValueError of string
  14. let BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  15. let println s =
  16.     printfn "%O" s
  17. let print s =
  18.     printf "%O" s
  19. let printUsage dummy =
  20.     println "Usage: MakeAsciiTable"
  21.     println "Make a table of ascii codes."
  22. let itoa (num : int, radix : int) =
  23.    let mutable isNegative = false
  24.    let mutable numx = num
  25.    if num < 0 then
  26.       isNegative <- true
  27.       numx <- (-num)
  28.    let mutable arr = [  ]
  29.    let mutable q = numx
  30.    let mutable r = 0
  31.    while (q >= radix) do
  32.        r <- int (q % radix)
  33.        q <- int (q / radix)
  34.        arr <- List.append arr [ sprintf "%O" (BASE36.[r]) ]
  35.    arr <- List.append arr [ sprintf "%O" (BASE36.[q]) ]
  36.    if isNegative then
  37.       arr <- List.append arr [ "-" ]
  38.    arr <- List.rev arr
  39.    System.String.Join("", List.toArray arr)
  40. let atoi (s : string, radix : int) : int =
  41.     let mutable ret = 0
  42.     let mutable isNegative = false
  43.     let len = s.Length
  44.     let mutable valx = 0
  45.     let mutable c = s.[0]
  46.     if c = '-' then
  47.         isNegative <- true
  48.     elif (c >= '0' && c <= '9') then
  49.         ret <- (int c) - (int '0')
  50.     elif (c >= 'A' && c <= 'Z') then
  51.         ret <- int c - int 'A' + 10
  52.     elif (c >= 'a' && c <= 'z') then
  53.         ret <- int c - int 'a' + 10
  54.     if (ret >= radix) then
  55.         printfn "    Error: Can not read \"%s\" (as radix %d): %O is an invalid character!" s radix ret
  56.     for i = 1 to len - 1 do
  57.         c <- s.[i]
  58.         ret <- ret*radix
  59.         if (c >= '0' && c <= '9') then
  60.             valx <- int c - int '0'
  61.         elif (c >= 'A' && c <= 'Z') then
  62.             valx <- int c - int 'A' + 10
  63.         elif (c >= 'a' && c <= 'z') then
  64.             valx <- int c - int 'a' + 10
  65.         if (valx >= radix) then
  66.             printfn "    Error: Can not read \"%s\" (as radix %d): %O is an invalid character!" s radix c

  67.         ret <- ret + valx
  68.     if (isNegative) then
  69.         ret <- (-ret )
  70.     ret
  71. let asc = [
  72.     "NUL"; "SOH"; "STX"; "ETX"; "EOT";
  73.     "ENQ"; "ACK"; "BEL"; "BS"; "HT";
  74.     "LF"; "VT"; "FF"; "CR"; "SO";
  75.     "SI"; "DLE"; "DC1"; "DC2"; "DC3";
  76.     "DC4"; "NAK"; "SYN"; "ETB"; "CAN";
  77.     "EM"; "SUB"; "ESC"; "FS"; "GS";
  78.     "RS"; "US"; "Spc"
  79. ]
  80. let control = [
  81.     "NUL (null)";
  82.     "SOH (start of heading)";
  83.     "STX (start of text)";
  84.     "ETX (end of text)";
  85.     "EOT (end of transmission)";
  86.     "ENQ (enquiry)";
  87.     "ACK (acknowledge)";
  88.     "BEL (bell)";
  89.     "BS  (backspace)";
  90.     "TAB (horizontal tab)";
  91.     "LF  (line feed, NL new line)";
  92.     "VT  (vertical tab)";
  93.     "FF  (form feed, NP new page)";
  94.     "CR  (carriage return)";
  95.     "SO  (shift out)";
  96.     "SI  (shift in)";
  97.     "DLE (data link escape)";
  98.     "DC1 (device control 1)";
  99.     "DC2 (device control 2)";
  100.     "DC3 (device control 3)";
  101.     "DC4 (device control 4)";
  102.     "NAK (negative acknowledge)";
  103.     "SYN (synchronous idle)";
  104.     "ETB (end of trans. block)";
  105.     "CAN (cancel)";
  106.     "EM  (end of medium)";
  107.     "SUB (substitute, EOF end of file)";
  108.     "ESC (escape)";
  109.     "FS  (file separator)";
  110.     "GS  (group separator)";
  111.     "RS  (record separator)";
  112.     "US  (unit separator)";
  113. ]
  114. let makeTable dummy =
  115.     let mutable sbuf = ""
  116.     let mutable abuf = ""
  117.     let mutable tbuf = ""
  118.     let mutable c = 'a'
  119.     sbuf <- "    "
  120.     for i in 0..(8-1) do
  121.         sbuf <- sbuf +  "+----"
  122.     sbuf <- sbuf +  "+"
  123.     println(sbuf)
  124.     sbuf <- "    "
  125.     sbuf <- sbuf +  "| 0- "
  126.     sbuf <- sbuf +  "| 1- "
  127.     sbuf <- sbuf +  "| 2- "
  128.     sbuf <- sbuf +  "| 3- "
  129.     sbuf <- sbuf +  "| 4- "
  130.     sbuf <- sbuf +  "| 5- "
  131.     sbuf <- sbuf +  "| 6- "
  132.     sbuf <- sbuf +  "| 7- "
  133.     sbuf <- sbuf +  "|"
  134.     println(sbuf)
  135.     sbuf <- "+---"
  136.     for i in 0..(8-1) do
  137.         sbuf <- sbuf +  "+----"
  138.     sbuf <- sbuf +  "+"
  139.     println(sbuf)
  140.     for i in 0..(16-1) do
  141.         tbuf <- ""
  142.         sbuf <- itoa(i, 16)
  143.         tbuf <- tbuf +  "| " + sbuf + " "
  144.         for j in 0..(8-1) do
  145.             if j*16 + i <= 32 then
  146.                 abuf <- sprintf "| %-3s" (asc.[j*16 + i])
  147.             elif j*16 + i = 127 then
  148.                 abuf <- sprintf  "| %-3s" "DEL"
  149.             else
  150.                 c <- char (j*16 + i)
  151.                 abuf <- sprintf "|  %1c " c
  152.             tbuf <- tbuf + abuf
  153.         tbuf <- tbuf + "|"
  154.         println(tbuf)
  155.     sbuf <- "+---"
  156.     for i in 0..(8-1) do
  157.         sbuf <- sbuf + "+----"
  158.     sbuf <- sbuf + "+"
  159.     println(sbuf)
  160.     println("")
  161.     for i in 0..(16-1) do
  162.         tbuf <- sprintf "%-30s  %-34s" (control.[i]) (control.[i+16])
  163.         println(tbuf)
  164. // Begin here
  165. let cmdArgs = System.Environment.GetCommandLineArgs()
  166. if (Array.length cmdArgs > 1 && "-h" = cmdArgs.[1]) then
  167.     printUsage 0
  168.     exit(1)
  169. makeTable 0




컴파일> fsc MakeAsciiTable.fs

실행> 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
,