Download Quick Currency Converter

Decimal to other Base Converter Visual Basic,

Description: This project converters a decimal number to a specified base. It uses the "repeated division by base" method to solve the problem. Note that the program works for a maximum of base-16 (and minimum base-2, ofcourse). It does no error checking for illegal bases, so this part is upto you.

Difficulty: Easy


Compiler/IDE: Visual Studio

View Source

Module Module1
    Dim n, x, p As Integer
    Dim s As String
    Dim c As Char

    'Base converter: Converts a decimal number to a given base...

    Sub Main()
        Console.WriteLine("Enter a decimal number")
        n = Console.ReadLine

        Console.WriteLine("Specify base to convert to")
        x = Console.ReadLine
        s = ""

        Call convert()

    End Sub

    Sub convert()

        Do Until n = 0
            p = n Mod x
            If p > 9 Then
                Call hex()
                s = c & s
                s = p & s
            End If
            n = n \ x


    End Sub

    Sub hex()
        Select Case p
            Case 10
                c = "A"
            Case 11
                c = "B"
            Case 12
                c = "C"
            Case 13
                c = "D"
            Case 14
                c = "E"
            Case 15
                c = "F"
        End Select
    End Sub

End Module

Notes:As mentioned earlier, the project uses "repeated division by base" method to convert numbers to other bases. This method works as follows: We divide the number by the specified base and then list the remainders in reverse order. If the base is greater than 10, we have A for 10, B for 11 and so on until 15 for F. The function "hex" does this job and is called only when the remainder is greater than 9.