프로그래밍/Julia
7비트 ASCII 코드표 만들기 예제 with Julia
Scripter
2013. 3. 5. 14:06
ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다. 이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다. ( 참고: ASCII - Wikipedia, the free encyclopedia )
다음은 7bit ASCII 코드표를 만들어 보여주는 Julia 소스 코드이다. 소스 코드 중에 진법변환에 필요한 함수
convertAtoI(string, radix)
convertItoA(number, radix)
의 구현도 포함되어 있다.
- ## Filename: makeAsciiTable.jl
- ## Make a table of ascii codes.
- ##
- ## Execute: julia makeAsciiTable.jl
- ##
- ## Date: 2013. 3. 5.
- ## Author: pkim __AT__ scripts ((DOT)) pe ((DOT)) kr
- BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- # The function println() is defined already in Julia language.
- # function println(s...)
- # if length(s) > 0
- # println(s[1])
- # else
- # println("")
- # end
- function printUsage()
- println("Usage: julia makeAsciiTable.jl")
- println("Make a table of ascii codes.")
- end
- function convertItoA(num, radix)
- isNegative = false
- if num < 0
- isNegative = true
- num = -num
- end
- arr = ""
- q, r = num, 0
- while q >= radix
- # q, r = divmod(q, radix)
- r = mod(q, radix)
- q = div(q, radix)
- arr *= BASE36[r+1]
- end
- arr *= BASE36[q + 1]
- if isNegative
- arr *= "-"
- end
- return reverse(arr)
- end
- function convertAtoI(srcStr, radix)
- isNegative = false
- ret = 0
- m = length(srcStr)
- val = 0
- c = srcStr[1]
- if c == '-'
- isNegative = true
- elseif c >= '0' && c <= '9'
- ret = int(c) - int('0')
- elseif c >= 'A' && c <= 'Z'
- ret = (int(c) - int('A')) + 10
- elseif c >= 'a' && c <= 'z'
- ret = (int(c) - int('a')) + 10
- end
- if ret >= radix
- println(" Invalid character!")
- return ret
- end
- for i = 2:m
- c = srcStr[i]
- ret *= radix
- if c >= '0' && c <= '9'
- val = int(c) - int('0')
- elseif c >= 'A' && c <= 'Z'
- val = (int(c) - int('A')) + 10
- elseif c >= 'a' && c <= 'z'
- val = (int(c) - int('a')) + 10
- end
- if val >= radix:
- println(" Invalid character!")
- return ret
- end
- ret *= val
- end
- return ret
- end
- 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"
- ]
- 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)",
- ]
- function toWidth(s, width)
- t = s
- if width > length(s)
- t = " "^(width - length(s)) * s
- elseif width < -length(s)
- t = s * " "^(-width - length(s))
- end
- return t
- end
- function makeTable()
- sbuf = ""
- abuf = ""
- tbuf = ""
- sbuf = " "
- for i = 1:8
- sbuf *= "+----"
- end
- sbuf *= "+"
- println(sbuf)
- sbuf = " "
- sbuf *= "| 0- "
- sbuf *= "| 1- "
- sbuf *= "| 2- "
- sbuf *= "| 3- "
- sbuf *= "| 4- "
- sbuf *= "| 5- "
- sbuf *= "| 6- "
- sbuf *= "| 7- "
- sbuf *= "|"
- println(sbuf)
- sbuf = "+---"
- for i = 1:8
- sbuf *= "+----"
- end
- sbuf *= "+"
- println(sbuf)
- for i = 0:15
- tbuf = ""
- sbuf = convertItoA(i, 16)
- tbuf *= "| " * sbuf * " "
- for j in 0:7
- if j*16 + i <= 32
- abuf = "| " * oWidth(asc[j*16 + i + 1], -3)
- elseif j*16 + i == 127
- abuf = "| " * "DEL"
- else
- c = char(j*16 + i)
- abuf = "| " * toWidth(string(c), 2) * " "
- end
- tbuf *= abuf
- end
- tbuf *= "|"
- println(tbuf)
- end
- sbuf = "+---"
- for i = 1:8
- sbuf *= "+----"
- end
- sbuf *= "+"
- println(sbuf)
- println("")
- for i = 0:15
- tbuf = toWidth(control[i+1], -30) * " " * toWidth(control[i +16 + 1], -34)
- println(tbuf)
- end
- end
- if length(ARGS) > 0 && "-h" == ARGS[1]
- printUsage()
- exit(1)
- end
- makeTable()
실행> julia makeAsciiTable.jl
+----+----+----+----+----+----+----+----+ | 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)