ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다. 이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다. ( 참고: ASCII - Wikipedia, the free encyclopedia )
다음은 7bit ASCII 코드표를 만들어 보여주는 Go 언어 소스 코드이다. 소스 코드 중에 진법변환에 필요한 함수
convertAtoI()
convertItoA()
의 구현도 포함되어 있다.
- /*
- * Filename: makeAsciiTable.go
- * Make a table of ascii codes.
- *
- * Execute: go run makeAsciiTable.go
- *
- * or
- *
- * Compile: go build makeAsciiTable.go
- * Execute: ./makeAsciiTable
- *
- * Date: 2012/06/25
- * Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- */
- package main
- import (
- "fmt"
- "os"
- )
- func println(s string) {
- fmt.Printf("%s\n", s)
- }
- func print(s string) {
- fmt.Printf("%s", s)
- }
- func printUsage() {
- println("Usage: makeAsciiTable")
- println("Make a table of ascii codes.")
- }
- func convertItoA(num int64, radix int) (ret string) {
- var BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- var tmp string
- var arr string
- var q, r int64
- var i, n int
- var isNegative bool = false
- if num < 0 {
- isNegative = true
- num = -num
- }
- arr = ""
- q = num
- r = 0
- for q >= int64(radix) {
- r = q % int64(radix)
- q = q / int64(radix)
- tmp = BASE36[r:r+1]
- arr += tmp
- }
- tmp = BASE36[q:q+1]
- arr += tmp
- if isNegative {
- arr += "-"
- }
- n = len(arr)
- for i = 0; i < n; i++ {
- ret += arr[n - i - 1:n-i]
- }
- return ret
- }
- func convertAtoI(str string, radix int) (ret int64) {
- var isNegative bool = false
- var len int = len(str)
- var c byte
- var i int
- var val int64
- c = str[0]
- if c == '-' {
- isNegative = true
- } else if c >= '0' && c <= '9' {
- ret = int64(c) - int64('0');
- } else if c >= 'A' && c <= 'Z' {
- ret = int64(c - 'A') + 10
- } else if c >= 'a' && c <= 'z' {
- ret = int64(c - 'a') + 10;
- }
- if ret >= int64(radix) {
- println(" Invalid character!")
- return ret
- }
- for i = 1; i < len; i++ {
- c = str[i]
- ret *= int64(radix)
- if c >= '0' && c <= '9' {
- val = int64(c - '0')
- } else if c >= 'A' && c <= 'Z' {
- val = int64(c - 'A') + 10
- } else if c >= 'a' && c <= 'z' {
- val = int64(c - 'a') + 10
- }
- if val >= int64(radix) {
- println(" Invalid character!")
- return ret
- }
- ret += val
- }
- if isNegative {
- ret = -ret
- }
- return ret
- }
- func makeTable() {
- var asc = [...] string {
- "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" }
- var control = [...]string{
- "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)" }
- var sbuf, abuf, tbuf string
- var i, j int
- var c byte
- print(" ");
- for i = 0; i < 8; i++ {
- print("+----")
- }
- print("+")
- println("")
- print(" ")
- print("| 0- ");
- print("| 1- ");
- print("| 2- ");
- print("| 3- ");
- print("| 4- ");
- print("| 5- ");
- print("| 6- ");
- print("| 7- ");
- print("|")
- println("")
- print("+---")
- for i = 0; i < 8; i++ {
- print("+----")
- }
- print("+")
- println("")
- for i = 0; i < 16; i++ {
- tbuf = ""
- sbuf = convertItoA(int64(i), 16)
- tbuf += "| "
- tbuf += sbuf
- tbuf += " "
- for j = 0; j < 8; j++ {
- if j*16 + i <= 32 {
- abuf = fmt.Sprintf("| %-3s", asc[j*16 + i])
- } else if (j*16 + i == 127) {
- abuf = fmt.Sprintf("| %-3s", "DEL")
- } else {
- c = byte(j*16 + i)
- abuf = fmt.Sprintf("| %2c ", c);
- }
- tbuf += abuf
- }
- tbuf += "|"
- println(tbuf)
- }
- print("+---")
- for i = 0; i < 8; i++ {
- print("+----")
- }
- print("+")
- println("")
- println("")
- for i = 0; i < 16; i++ {
- sbuf = fmt.Sprintf("%-30s", control[i])
- tbuf = fmt.Sprintf(" %-34s", control[i+16])
- print(sbuf)
- println(tbuf)
- }
- }
- func main() {
- if len(os.Args) > 1 && os.Args[1] == "-h" {
- printUsage()
- os.Exit(1)
- }
- makeTable()
- }
실행> go run makeAsciiTable.go
또는
컴파일> go build makeAsciiTable.go
실행> ./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)
'프로그래밍 > Go' 카테고리의 다른 글
Go 언어로 3제곱근, 4제곱근, n제곱근 구하기 (0) | 2012.06.27 |
---|---|
삼각형 출력 예제를 통한 여러 가지 소스 비교 with Go (0) | 2012.06.26 |
진법(radix) 표 만들기 예제 with Go (0) | 2012.06.25 |
대화형 모드의 진법(radix) 변환 예제 with Go (0) | 2012.06.22 |
황금비율(golden ratio) 구하기 with Go (0) | 2012.06.20 |