컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은 0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 Boo 소스 코드이다. 진법 변환에 필요한 함수
convertAtoI(string, radix)
convertItoA(number, radix)
를 Boo 코드로 자체 작성하여 사용하였다.
- # Filename: makeRadixTable.boo
- # Show the radix table with 10-, 2-, 8-, 16-radices.
- #
- # Execute: booi makeRadixTable.boo
- #
- # Date: 2009/04/03
- # Author: PH Kim [ pkim (AT) scripts.pe.kr ]
- import System
- BASE36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- def println(s as string):
- print(s)
- def printUsage():
- println("Usage: booi makeRadixTable.boo")
- println("Show the radix table with 10-, 2-, 8-, 16-radices.")
- def convertItoA(num as long, radix as int) as string:
- isNegative = false
- if num < 0:
- isNegative = true
- num = -num
- arr as string = ""
- q as long = num
- r as long = 0
- while q >= radix:
- r = q % radix
- q = q / radix
- arr += BASE36[r]
- arr += BASE36[q]
- if isNegative:
- arr += "-"
- s as string = ""
- n = len(arr)
- for i as int in range(n):
- s += arr[n - 1 - i]
- return s
- def toAscii(s as String) as int:
- objAscii as System.Text.ASCIIEncoding = System.Text.ASCIIEncoding()
- val as System.Byte = objAscii.GetBytes(s)[0]
- return Convert.ToInt32(val)
- def convertAtoI(srcStr as string, radix as int) as long:
- isNegative = false
- ret as long = 0L
- m = len(srcStr)
- val = 0
- c = srcStr[0:1]
- if toAscii(c) == toAscii('-'):
- isNegative = true
- elif toAscii(c) >= toAscii('0') and toAscii(c) <= toAscii('9'):
- ret = toAscii(c) - toAscii('0')
- elif toAscii(c) >= toAscii('A') and toAscii(c) <= toAscii('Z'):
- ret = (toAscii(c) - toAscii('A')) + 10
- elif toAscii(c) >= toAscii('a') and toAscii(c) <= toAscii('z'):
- ret = (toAscii(c) - toAscii('a')) + 10
- if ret >= radix:
- raise Exception(" invalid character!")
- return ret
- for i in range(1, m):
- c = srcStr[i:i+1]
- ret *= radix
- if toAscii(c) >= char('0') and toAscii(c) <= char('9'):
- val = toAscii(c) - toAscii('0')
- elif toAscii(c) >= char('A') and toAscii(c) <= char('Z'):
- val = (toAscii(c) - toAscii('A')) + 10
- elif toAscii(c) >= char('a') and toAscii(c) <= char('z'):
- val = (toAscii(c) - toAscii('a')) + 10
- if val >= radix:
- raise Exception(" invalid character!")
- return ret
- ret += val
- return ret
- def makeTable():
- sbuf = ""
- abuf = ""
- tbuf = ""
- for i in range(0, 4):
- sbuf += "+-------"
- sbuf += "+"
- println(sbuf)
- sbuf = "| Dec"
- sbuf += "\t| Bin"
- sbuf += "\t| Oct"
- sbuf += "\t| Hex |"
- println(sbuf)
- sbuf = ""
- for i in range(0, 4):
- sbuf += "+-------"
- sbuf += "+"
- println(sbuf)
- for i in range(0, 16):
- sbuf = string.Format("| {0,2}", i)
- abuf = convertItoA(i, 2)
- tbuf = string.Format("\t| {0,4}", abuf)
- sbuf += tbuf
- abuf = convertItoA(i, 8)
- tbuf = string.Format("\t| {0,2}", abuf)
- sbuf += tbuf
- abuf = convertItoA(i, 16)
- tbuf = string.Format("\t| {0,-2} |", abuf)
- sbuf += tbuf
- println(sbuf)
- sbuf = ""
- for i in range(0, 4):
- sbuf += "+-------"
- sbuf += "+"
- println(sbuf)
- if len(argv) > 0 and "-h" == argv[0]:
- printUsage()
- Environment.Exit(1)
- makeTable()
실행> booi makeRadixTable.boo
+-------+-------+-------+-------+ | Dec | Bin | Oct | Hex | +-------+-------+-------+-------+ | 0 | 0 | 0 | 0 | | 1 | 1 | 1 | 1 | | 2 | 10 | 2 | 2 | | 3 | 11 | 3 | 3 | | 4 | 100 | 4 | 4 | | 5 | 101 | 5 | 5 | | 6 | 110 | 6 | 6 | | 7 | 111 | 7 | 7 | | 8 | 1000 | 10 | 8 | | 9 | 1001 | 11 | 9 | | 10 | 1010 | 12 | A | | 11 | 1011 | 13 | B | | 12 | 1100 | 14 | C | | 13 | 1101 | 15 | D | | 14 | 1110 | 16 | E | | 15 | 1111 | 17 | F | +-------+-------+-------+-------+
'프로그래밍 > Boo' 카테고리의 다른 글
문자열 거꾸로 하기 with Boo (0) | 2009.04.04 |
---|---|
7비트 ASCII 코드표 만들기 예제 with Boo (0) | 2009.04.03 |
대화형 모드의 진법(radix) 변환 예제 with Boo (0) | 2009.04.03 |
황금비율(golden ratio) 구하기 with Boo (0) | 2009.04.01 |
현재 시각 알아내기 for Boo (0) | 2009.04.01 |