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# 언어의 명령형 언어 특징을 위주로 작성되어 있다.)
- (*
- * Filename: MakeAsciiTable.fs
- * Make a table of ascii codes.
- *
- * Compile: fsc MakeAsciiTable.fs
- * Execute: MakeAsciiTable
- *
- * Date: 20010/07/15
- * Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- *)
- #light
- exception RuntimeError of string
- exception ValueError of string
- let BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- let println s =
- printfn "%O" s
- let print s =
- printf "%O" s
- let printUsage dummy =
- println "Usage: MakeAsciiTable"
- println "Make a table of ascii codes."
- let itoa (num : int, radix : int) =
- let mutable isNegative = false
- let mutable numx = num
- if num < 0 then
- isNegative <- true
- numx <- (-num)
- let mutable arr = [ ]
- let mutable q = numx
- let mutable r = 0
- while (q >= radix) do
- r <- int (q % radix)
- q <- int (q / radix)
- arr <- List.append arr [ sprintf "%O" (BASE36.[r]) ]
- arr <- List.append arr [ sprintf "%O" (BASE36.[q]) ]
- if isNegative then
- arr <- List.append arr [ "-" ]
- arr <- List.rev arr
- System.String.Join("", List.toArray arr)
- let atoi (s : string, radix : int) : int =
- let mutable ret = 0
- let mutable isNegative = false
- let len = s.Length
- let mutable valx = 0
- let mutable c = s.[0]
- if c = '-' then
- isNegative <- true
- elif (c >= '0' && c <= '9') then
- ret <- (int c) - (int '0')
- elif (c >= 'A' && c <= 'Z') then
- ret <- int c - int 'A' + 10
- elif (c >= 'a' && c <= 'z') then
- ret <- int c - int 'a' + 10
- if (ret >= radix) then
- printfn " Error: Can not read \"%s\" (as radix %d): %O is an invalid character!" s radix ret
- for i = 1 to len - 1 do
- c <- s.[i]
- ret <- ret*radix
- if (c >= '0' && c <= '9') then
- valx <- int c - int '0'
- elif (c >= 'A' && c <= 'Z') then
- valx <- int c - int 'A' + 10
- elif (c >= 'a' && c <= 'z') then
- valx <- int c - int 'a' + 10
- if (valx >= radix) then
- printfn " Error: Can not read \"%s\" (as radix %d): %O is an invalid character!" s radix c
ret <- ret + valx- if (isNegative) then
- ret <- (-ret )
- ret
- let asc = [
- "NUL"; "SOH"; "STX"; "ETX"; "EOT";
- "ENQ"; "ACK"; "BEL"; "BS"; "HT";
- "LF"; "VT"; "FF"; "CR"; "SO";
- "SI"; "DLE"; "DC1"; "DC2"; "DC3";
- "DC4"; "NAK"; "SYN"; "ETB"; "CAN";
- "EM"; "SUB"; "ESC"; "FS"; "GS";
- "RS"; "US"; "Spc"
- ]
- let control = [
- "NUL (null)";
- "SOH (start of heading)";
- "STX (start of text)";
- "ETX (end of text)";
- "EOT (end of transmission)";
- "ENQ (enquiry)";
- "ACK (acknowledge)";
- "BEL (bell)";
- "BS (backspace)";
- "TAB (horizontal tab)";
- "LF (line feed, NL new line)";
- "VT (vertical tab)";
- "FF (form feed, NP new page)";
- "CR (carriage return)";
- "SO (shift out)";
- "SI (shift in)";
- "DLE (data link escape)";
- "DC1 (device control 1)";
- "DC2 (device control 2)";
- "DC3 (device control 3)";
- "DC4 (device control 4)";
- "NAK (negative acknowledge)";
- "SYN (synchronous idle)";
- "ETB (end of trans. block)";
- "CAN (cancel)";
- "EM (end of medium)";
- "SUB (substitute, EOF end of file)";
- "ESC (escape)";
- "FS (file separator)";
- "GS (group separator)";
- "RS (record separator)";
- "US (unit separator)";
- ]
- let makeTable dummy =
- let mutable sbuf = ""
- let mutable abuf = ""
- let mutable tbuf = ""
- let mutable c = 'a'
- sbuf <- " "
- for i in 0..(8-1) do
- sbuf <- sbuf + "+----"
- sbuf <- sbuf + "+"
- println(sbuf)
- sbuf <- " "
- sbuf <- sbuf + "| 0- "
- sbuf <- sbuf + "| 1- "
- sbuf <- sbuf + "| 2- "
- sbuf <- sbuf + "| 3- "
- sbuf <- sbuf + "| 4- "
- sbuf <- sbuf + "| 5- "
- sbuf <- sbuf + "| 6- "
- sbuf <- sbuf + "| 7- "
- sbuf <- sbuf + "|"
- println(sbuf)
- sbuf <- "+---"
- for i in 0..(8-1) do
- sbuf <- sbuf + "+----"
- sbuf <- sbuf + "+"
- println(sbuf)
- for i in 0..(16-1) do
- tbuf <- ""
- sbuf <- itoa(i, 16)
- tbuf <- tbuf + "| " + sbuf + " "
- for j in 0..(8-1) do
- if j*16 + i <= 32 then
- abuf <- sprintf "| %-3s" (asc.[j*16 + i])
- elif j*16 + i = 127 then
- abuf <- sprintf "| %-3s" "DEL"
- else
- c <- char (j*16 + i)
- abuf <- sprintf "| %1c " c
- tbuf <- tbuf + abuf
- tbuf <- tbuf + "|"
- println(tbuf)
- sbuf <- "+---"
- for i in 0..(8-1) do
- sbuf <- sbuf + "+----"
- sbuf <- sbuf + "+"
- println(sbuf)
- println("")
- for i in 0..(16-1) do
- tbuf <- sprintf "%-30s %-34s" (control.[i]) (control.[i+16])
- println(tbuf)
- // Begin here
- let cmdArgs = System.Environment.GetCommandLineArgs()
- if (Array.length cmdArgs > 1 && "-h" = cmdArgs.[1]) then
- printUsage 0
- exit(1)
- 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)
'프로그래밍 > F#' 카테고리의 다른 글
문자열 거꾸로 하기 with F# (0) | 2010.07.15 |
---|---|
손으로 만드는 나눗셈 계산표 with F# (0) | 2010.07.15 |
진법(radix) 표 만들기 예제 with F# (0) | 2010.07.15 |
대화형 모드의 진법(radix) 변환 예제 with F# (0) | 2010.07.14 |
황금비율(golden ratio) 구하기 with F# (0) | 2010.07.13 |