ASCII(애스키)란 American Standard Code for Information Interchange의 줄임글로서, 영문자에 기초한 문자 인코딩이다. 이 문자 인코딩에는 C0 제어문자(C0 control character)도 포함되어 있다. ( 참고: ASCII - Wikipedia, the free encyclopedia )
다음은 7bit ASCII 코드표를 만들어 보여주는 Visual Basic 소스 코드이다. 소스 코드 중에 진법변환에 필요한 함수
ConvertAtoI(string, radix)
ConvertItoA(long, radix)
의 구현도 포함되어 있다.
' Make a table of ascii codes.
'
' Compile: vbc MakeAsciiTableApp.bas
' Execute: MakeAsciiTableApp
'
' Date: 2009/02/14
' Author: PH Kim [ pkim (AT) scripts.pe.kr ]
Imports System
Namespace MyTestApplication1
Class MakeAsciiTableApp
Shared Sub println(s As String)
Console.WriteLine(s)
End Sub
Shared Sub print(s As String)
Console.Write(s)
End Sub
Shared Sub PrintUsage()
println("Usage: MakeAsciiTableApp")
println("Make a table of ascii codes.")
End Sub
Shared Dim BASE36 As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Shared Function ConvertItoA(num As Int64, radix As Int32) As String
Dim tmp As String
Dim ret As String
Dim arr As String
Dim q As Int64
Dim r As Int64
Dim isNegative As Boolean = False
If num < 0L Then
isNegative = True
num = -num
End If
arr = ""
q = num
r = 0L
While q >= Convert.ToInt64(radix)
r = q Mod Convert.ToInt64(radix)
q = Convert.ToInt64(Math.Floor(q / Convert.ToInt64(radix)))
tmp = Mid(BASE36, Convert.ToInt32(r) + 1, 1)
arr = arr & tmp
End While
tmp = Mid(BASE36, Convert.ToInt32(q) + 1, 1)
arr = arr & tmp
If isNegative Then
arr = arr & "-"
End If
ret = ""
For j As Integer = 0 To arr.Length - 1
ret = ret & Mid(arr, arr.Length - j - 1 + 1, 1)
Next
ConvertItoA = ret
End Function
Shared Function ConvertAtoI(s As String, radix As Integer) As Int64
Dim ret As Int64 = 0L
Dim isNegative As Boolean = False
Dim len As Integer = s.Length
Dim c As char
Dim i As Integer
Dim val As Int64 = 0L
c = s(0)
If c = "-"c Then
isNegative = True
ElseIf c >= "0"c And c <= "9"c Then
ret = Convert.ToInt64(c) - Convert.ToInt64("0"c)
ElseIf c >= "A"c And c <= "Z"c Then
ret = Convert.ToInt64(c) - Convert.ToInt64("A"c) + 10L
ElseIf c >= "a"c And c <= "z"c Then
ret = Convert.ToInt64(c) - Convert.ToInt64("a"c) + 10L
End If
If (ret >= Convert.ToInt64(radix)) Then
Console.WriteLine(" Invalid character!")
ConvertAtoI = ret
Return ret
End If
For i = 1 To len - 1
c = s(i)
ret = ret * radix
If c >= "0"c And c <= "9"c Then
val = Convert.ToInt64(c) - Convert.ToInt64("0"c)
ElseIf c >= "A"c And c <= "Z"c Then
val = Convert.ToInt64(c) - Convert.ToInt64("A"c) + 10L
ElseIf c >= "a"c And c <= "z"c Then
val = Convert.ToInt64(c) - Convert.ToInt64("a"c) + 10L
End If
If (val >= Convert.ToInt64(radix)) Then
Console.WriteLine(" Invalid character!")
ConvertAtoI = ret
Return ret
End If
ret = ret + val ' numerical addition
Next
If isNegative Then
ret = -ret
End If
ConvertAtoI = ret
return ret
End Function
Shared Dim asc() As String = New 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" _
}
Shared Dim control() As String = New 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)" _
}
Shared Sub MakeTable()
Dim sbuf As String = ""
Dim abuf As String = ""
Dim tbuf As String = ""
Dim i As Integer
Dim j As Integer
Dim c As char
print(" ")
For i = 0 To 7
print("+----")
Next
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 To 7
print("+----")
Next
print("+")
println("")
For i = 0 To 15
tbuf = ""
sbuf = ConvertItoA(i, 16)
tbuf = tbuf + "| " & sbuf & " "
For j = 0 To 7
If j*16 + i <= 32 Then
abuf = String.Format("| {0, -3}", asc(j*16 + i))
ElseIf j*16 + i = 127 Then
abuf = String.Format("| {0, -3}", "DEL")
Else
c = Convert.ToChar(j*16 + i)
abuf = String.Format("| {0, 2} ", c)
End if
tbuf = tbuf & abuf
Next
tbuf = tbuf & "|"
println(tbuf)
Next
print("+---")
For i = 0 To 7
print("+----")
Next
print("+")
println("")
println("")
For i = 0 To 15
sbuf = String.Format("{0, -30}", control(i))
tbuf = String.Format(" {0, -34}", control(i+16))
print(sbuf)
println(tbuf)
Next
End Sub
' Java 언어의 main 메소드에 해당하는 Visual Basic의 Main
Shared Sub Main(ByVal args() As String)
If args.Length = 0 Then
'
ElseIf args.Length > 0 And "-h" = args(0) Then
PrintUsage()
Environment.Exit(1)
End If
MakeTable()
End Sub
End Class
End Namespace
컴파일> vbc MakAsciiTableApp.bas
실행> MakeAsciiTableApp
+----+----+----+----+----+----+----+----+ | 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)
'프로그래밍 > BASIC' 카테고리의 다른 글
손으로 만드는 나눗셈 계산표 with Visual Basic (0) | 2009.02.16 |
---|---|
대화형 모드의 진법(radix) 변환 예제 with Visual Basic (0) | 2009.02.16 |
진법(radix) 표 만들기 예제 with Visual Basic (0) | 2009.02.16 |
구구단 출력 예제 for Visual Basic (0) | 2009.02.14 |
문자열 거꾸로 하기 with Visual Basic (0) | 2009.02.14 |