컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은  0부터 15까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 Visual Basic 소스 코드이다. 진법 변환에 필요한 Visual Basic 함수로

         Convert.Int64(str As String,  radix As Integer)

가 이미 있지만, 여기에서는 아예 변환 함수

        ConvertAtoI(string, radix)
        ConvertItoA(long, radix)

를 자체 구현하여 사용하였다.


'  Filename: MakeRadixTableApp.bas
'            Show the radix table with 10-, 2-, 8-, 16-radices.
'
'  Compile: vbc MakeRadixTableApp.bas
'  Execute: MakeRadixTableApp
'
'      Date:  2009/02/14
'    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]

Imports System

Namespace MyTestApplication1

    Class MakeRadixTableApp

        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: MakeRadixTableApp")
            println("Show the radix table with 10-, 2-, 8-, 16-radices.")
        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 Sub MakeTable()
            Dim sbuf As String = ""
            Dim abuf As String = ""
            Dim tbuf As String = ""
            Dim i As Integer
            Dim tab As Char = Chr(9)

            For i = 0 To 3
                print("+-------")
            Next
            print("+")
            println("")

            print("|  Dec")
            print(tab & "|   Bin")
            print(tab & "|  Oct")
            print(tab & "|  Hex  |")
            println("")

            For i = 0 To 3
                print("+-------")
            Next
            print("+")
            println("")

            For i = 0 To 15
                sbuf = String.Format("|   {0, 2}", i)
                abuf = ConvertItoA(i, 2)
                tbuf = String.Format(tab & "|  {0, 4}", abuf)
                sbuf = sbuf & tbuf
                abuf = ConvertItoA(i, 8)
                tbuf = String.Format(tab & "|   {0, 2}", abuf)
                sbuf = sbuf & tbuf
                abuf = ConvertItoA(i, 16)
                tbuf = String.Format(tab & "|    {0, -2} |", abuf)
                sbuf = sbuf & tbuf
                println(sbuf)
            Next

            For i = 0 To 3
                print("+-------")
            Next
            print("+")
            println("")
        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 MakeRadixTableApp.bas

실행> MakeRadixTableApp

+-------+-------+-------+-------+
|  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  |
+-------+-------+-------+-------+



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,