ASCII(애스키)란 American Standard Cpde for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다. 이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다. ( 참고: ASCII - Wikipedia, the free encyclopedia )
다음은 7bit ASCII 코드표를 만들어 보여주는 Io 소스 코드이다.
소스 코드 중에 진법변환에 필요한 함수
convertAtoI(String, radix)
convertItoA(long, radix)
의 구현도 포함되어 있다.
- /*
- * Filename: makeAsciiTable.io
- * Make a table of ascii codes.
- *
- * Execute: io makeAsciiTable.io
- *
- * Date: 2008/05/02
- * Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- */
- printUsage := method(
- "Usage: io makeAsciiTable.io" println
- "Make a table of ascii codes." println
- )
- convertItoA := method(num, radix,
- BASE36 := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- q := 0
- r := 0
- isNegative := false
- if (num < 0) then (
- isNegative = true
- num = -num
- )
- arr := ""
- q = num
- r = 0
- while (q >= radix,
- r = q % radix
- q = (q / radix) floor
- arr = arr .. (BASE36 slice(r, r + 1))
- )
- arr = arr .. (BASE36 slice(q, q + 1))
- if (isNegative) then (
- arr = arr .. "-"
- )
- n := arr size
- ret := ""
- for (i, 0, n - 1,
- ret = ret .. arr slice(n - i - 1, n - i)
- )
- return ret
- )
- convertAtoI := method(srcStr, radix,
- BASE36 := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- isNegative := false
- ret := 0
- len := srcStr size
- c := ""
- i := 0
- val := 0
- c = srcStr at(0)
- if (c == "-") then (
- isNegative = true
- ) elseif (c >= "0" and c <= "9") then (
- ret = (c product) - ("0" product)
- ) elseif (c >= "A" and c <= "Z") then (
- ret = (c product) - ("A" product) + 10
- ) elseif (c >= "a" and <= "z") then (
- ret = (c product) - ("a" product) + 10
- )
- if (ret >= adix) then (
- " Invalid character!" println
- return ret
- )
- for (i, 1, len - 1,
- c = srcStr[i]
- ret = ret * radix
- if (c >= "0" and c <= "9") then (
- val = c product - "0" product
- ) elseif (c >= 'A' and c <= 'Z') then (
- val = c product - "A" product + 10
- ) elseif (c >= 'a' and c <= 'z') then (
- val = c product - "a" product + 10
- )
- if (val >= (long) radix) then (
- " Invalid character!" println
- return ret
- )
- ret = ret + val
- )
- return ret
- )
- makeTable := method(
- asc := list( "NUL", "SOH", "STX", "ETX", "EOT" )
- asc append( "ENQ", "ACK", "BEL", "BS", "HT" )
- asc append( "LF", "VT", "FF", "CR", "SO" )
- asc append( "SI", "DLE", "DC1", "DC2", "DC3" )
- asc append( "DC4", "NAK", "SYN", "ETB", "CAN" )
- asc append( "EM", "SUB", "ESC", "FS", "GS" )
- asc append( "RS", "US", "Spc" )
- control := list( "NUL (null)" )
- control append( "SOH (start of heading)" )
- control append( "STX (start of text)" )
- control append( "ETX (end of text)" )
- control append( "EOT (end of transmission)" )
- control append( "ENQ (enquiry)" )
- control append( "ACK (acknowledge)" )
- control append( "BEL (bell)" )
- control append( "BS (backspace)" )
- control append( "TAB (horizontal tab)" )
- control append( "LF (line feed, NL new line)" )
- control append( "VT (vertical tab)" )
- control append( "FF (form feed, NP new page)" )
- control append( "CR (carriage return)" )
- control append( "SO (shift out)" )
- control append( "SI (shift in)" )
- control append( "DLE (data link escape)" )
- control append( "DC1 (device control 1)" )
- control append( "DC2 (device control 2)" )
- control append( "DC3 (device control 3)" )
- control append( "DC4 (device control 4)" )
- control append( "NAK (negative acknowledge)" )
- control append( "SYN (synchronous idle)" )
- control append( "ETB (end of trans. block)" )
- control append( "CAN (cancel)" )
- control append( "EM (end of medium)" )
- control append( "SUB (substitute, EOF end of file)" )
- control append( "ESC (escape)" )
- control append( "FS (file separator)" )
- control append( "GS (group separator)" )
- control append( "RS (record separator)" )
- control append( "US (unit separator)" )
- sbuf := ""
- abuf := ""
- tbuf := ""
- i := 0
- j := 0
- c := ""
- for (i, 0, 7,
- )
- "" println
- "| 0- " print
- "| 1- " print
- "| 2- " print
- "| 3- " print
- "| 4- " print
- "| 5- " print
- "| 6- " print
- "| 7- " print
- "" println
- for (i, 0, 7,
- )
- "" println
- tmpS := ""
- for (i, 0, 15,
- tbuf = ""
- sbuf = convertItoA(i, 16)
- tbuf = tbuf .. "| " .. sbuf .. " "
- for (j, 0, 7,
- if (j*16 + i <= 32) then (
- tmpS = asc at(j*16 + i)
- if (tmpS size < 3) then ( tmpS = tmpS .. (" " repeated(3 - (tmpS size)) ) )
- abuf = "| " .. tmpS
- ) elseif (j*16 + i == 127) then (
- tmpS = "DEL"
- if (tmpS size < 3) then ( tmpS = tmpS .. (" " repeated(3 - (tmpS size)) ) )
- abuf = "| " .. tmpS
- ) else (
- c := Sequence clone
- c append(j*16 + i)
- tmpS = c asString
- if (tmpS size < 2) then ( tmpS = (" " repeated(2 - (tmpS size)) .. tmpS ) )
- abuf = "| " .. tmpS .. " "
- )
- tbuf = tbuf .. abuf
- )
- tbuf = tbuf .. "|"
- tbuf println
- )
- for (i, 0, 7,
- )
- "" println
- "" println
- for (i, 0, 15,
- // sbuf = String.format("%-30s", control at(i)
- // tbuf = String.format(" %-34s", control at(i+16))
- tmpS = control at(i)
- if (tmpS size < 30) then ( tmpS = tmpS .. (" " repeated(30 - (tmpS size)) ) )
- sbuf = tmpS
- tmpS = control at(i+16)
- if (tmpS size < 34) then ( tmpS = tmpS .. (" " repeated(34 - (tmpS size)) ) )
- tbuf = tmpS
- sbuf print
- tbuf println
- )
- )
- if (args size > 1 and "-h" == args at(1)) then (
- printUsage()
- exit(1)
- )
- makeTable()
실행> io makeAsciiTable.io
+----+----+----+----+----+----+----+----+ | 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)
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
'프로그래밍 > Io' 카테고리의 다른 글
손으로 만드는 나눗셈 계산표 with Io (0) | 2008.05.16 |
---|---|
삼각형 출력 예제를 통한 여러 가지 소스 비교 with Io (0) | 2008.05.02 |
진법(radix) 표 만들기 예제 with Io (0) | 2008.05.01 |
대화형 모드의 진법(radix) 변환 예제 with Io (0) | 2008.04.30 |
황금비율(golden ratio) 구하기 with Io (0) | 2008.04.15 |