ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다. 이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다. ( 참고: ASCII - Wikipedia, the free encyclopedia )
다음은 7bit ASCII 코드표를 만들어 보여주는 Python 소스 코드이다. 소스 코드 중에 진법변환에 필요한 함수
convertAtoI(string, radix)
convertItoA(number, radix)
의 구현도 포함되어 있다.
(아래의 소스는 Jython이나 IronPython에서도 수정없이 그대로 실행된다.)
- # Filename: makeAsciiTable.py
- # Make a table of ascii codes.
- #
- # Execute: python makeAsciiTable.py
- #
- # Date: 2008/03/28
- # Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- import sys
- BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- def println(s=None):
- if s == None:
- else:
- print(s)
- def printUsage():
- println("Usage: python makeAsciiTable.py")
- println("Make a table of ascii codes.")
- def convertItoA(num, radix):
- isNegative = False
- if num < 0:
- isNegative = True
- num = -num
- arr = ""
- q = num
- r = 0
- while q >= radix:
- r = q % radix
- q = q / radix
- arr += BASE36[r]
- arr += BASE36[q]
- if isNegative:
- arr += "-"
- n = len(arr)
- ret = ""
- for i in range(0, n):
- ret += arr[n - i - 1]
- return ret
- def convertAtoI(srcStr, radix):
- isNegative = False
- ret = 0
- m = len(srcStr)
- val = 0
- c = srcStr[0]
- if c == '-':
- isNegative = True
- elif c >= '0' and c <= '9':
- ret = ord(c) - ord('0')
- elif c >= 'A' and c <= 'Z':
- ret = (ord(c) - ord('A')) + 10
- elif c >= 'a' and c <= 'z':
- ret = (ord(c) - ord('a')) + 10
- if ret >= radix:
- println(" Invalid character!")
- return ret
- for i in range(1, m):
- c = srcStr[i]
- ret *= radix
- if c >= '0' and c <= '9':
- val = ord(c) - ord('0')
- elif c >= 'A' and c <= 'Z':
- val = (ord(c) - ord('A')) + 10
- elif c >= 'a' and c <= 'z':
- val = (ord(c) - ord('a')) + 10
- if val >= radix:
- println(" Invalid character!")
- return ret
- ret += val
- return ret
- 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)",
- ]
- def makeTable():
- sbuf = ""
- abuf = ""
- tbuf = ""
- sbuf = " "
- for i in range(0, 8):
- sbuf += "+----"
- 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 in range(0, 8):
- sbuf += "+----"
- sbuf += "+"
- println(sbuf)
- for i in range(0, 16):
- tbuf = ""
- sbuf = convertItoA(i, 16)
- tbuf += "| " + sbuf + " "
- for j in range(0, 8):
- if j*16 + i <= 32:
- abuf = "| %-3s" % asc[j*16 + i]
- elif j*16 + i == 127:
- abuf = "| %-3s" % "DEL"
- else:
- c = chr(j*16 + i)
- abuf = "| %2c " % c
- tbuf += abuf
- tbuf += "|"
- println(tbuf)
- sbuf = "+---"
- for i in range(0, 8):
- sbuf += "+----"
- sbuf += "+"
- println(sbuf)
- println("")
- for i in range(0, 16):
- tbuf = "%-30s %-34s" % (control[i], control[i+16])
- println(tbuf)
- if len(sys.argv) > 1 and "-h" == sys.argv[1]:
- printUsage()
- sys.exit(1)
- makeTable()
실행> python makeAsciiTable.py
+----+----+----+----+----+----+----+----+ | 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 대한민국 라이센스에 따라 이용하실 수 있습니다.
'프로그래밍 > Python' 카테고리의 다른 글
삼각형 출력 예제를 통한 여러 가지 소스 비교 with Python (0) | 2008.04.05 |
---|---|
Python에서 공백 문자 없이 연속적으로 출력하려면 (0) | 2008.04.04 |
진법(radix) 표 만들기 예제 with Python (or Jython or IronPython) (0) | 2008.03.29 |
대화형 모드의 진법(radix) 변환 예제 with Python (0) | 2008.03.28 |
황금비율(golden ratio) 구하기 with Python or Jython (0) | 2008.03.24 |