Base Converter

Class TNumericConverter provides the set of function to convert VB.NET integer typed values to its representation in system with arbitary base. Since binary and hexadecimal systems are very popular between programmers I provided functions expliciltly supporting them.

sample output

 
'
' 
'  NumericConverter.vb - functions to perform base conversion
'
'  Project:
'  Author: S.Zabinskis
'  June, 2007
'
'

Public Class TNumericConverter

    ' max. radix=36
    Private Shared Alphabet() As Char = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", _
    "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}

    Public Shared Function Reverse(ByVal str As String) As String
        Dim rstr As String = String.Empty
        For j As Integer = 1 To str.Length
            rstr &= str.Substring(str.Length - j, 1)
        Next
        Return rstr
    End Function


    Public Shared Function TrimTrailingZeros(ByVal str As String) As String
        For j As Integer = 1 To str.Length
            If str.Substring(str.Length - j, 1) <> "0" Then
                Return str.Substring(0, str.Length - j + 1)
            End If
        Next
        Return "0"
    End Function

    Public Shared Function BytesToBasedStr(ByVal data() As Byte, ByVal radix As Integer) As String
        Dim buffer() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
        For l As Integer = 0 To data.Length - 1
            buffer(l) = data(l)
        Next

        Dim value As ULong = BitConverter.ToUInt64(buffer, 0)
        Dim str As String = String.Empty
        While value > 0
            Dim coeff As Integer = value Mod radix
            str &= TNumericConverter.Alphabet(coeff)
            value \= radix
        End While
        Return Reverse(TrimTrailingZeros(str))
    End Function


    Public Shared Function ToBasedString(ByVal N As Short, ByVal radix As Integer) As String
        Return BytesToBasedStr(BitConverter.GetBytes(N), radix)
    End Function


    Public Shared Function ToBasedString(ByVal N As UShort, ByVal radix As Integer) As String
        Return BytesToBasedStr(BitConverter.GetBytes(N), radix)
    End Function


    Public Shared Function ToBasedString(ByVal N As Integer, ByVal radix As Integer) As String
        Return BytesToBasedStr(BitConverter.GetBytes(N), radix)
    End Function


    Public Shared Function ToBasedString(ByVal N As UInteger, ByVal radix As Integer) As String
        Return BytesToBasedStr(BitConverter.GetBytes(N), radix)
    End Function


    Public Shared Function ToBasedString(ByVal N As Long, ByVal radix As Integer) As String
        Return BytesToBasedStr(BitConverter.GetBytes(N), radix)
    End Function


    Public Shared Function ToBasedString(ByVal N As ULong, ByVal radix As Integer) As String
        Return BytesToBasedStr(BitConverter.GetBytes(N), radix)
    End Function



    ' special case - hexadecimal representation
    Public Shared Function ToHexString(ByVal N As Short) As String
        Return ToBasedString(N, 16)
    End Function


    Public Shared Function ToHexString(ByVal N As UShort) As String
        Return ToBasedString(N, 16)
    End Function


    Public Shared Function ToHexString(ByVal N As Integer) As String
        Return ToBasedString(N, 16)
    End Function


    Public Shared Function ToHexString(ByVal N As UInteger) As String
        Return ToBasedString(N, 16)
    End Function


    Public Shared Function ToHexString(ByVal N As Long) As String
        Return ToBasedString(N, 16)
    End Function


    Public Shared Function ToHexString(ByVal N As ULong) As String
        Return ToBasedString(N, 16)
    End Function


    ' special case - binary representation
    Public Shared Function ToBinString(ByVal N As Short) As String
        Return ToBasedString(N, 2)
    End Function


    Public Shared Function ToBinString(ByVal N As UShort) As String
        Return ToBasedString(N, 2)
    End Function


    Public Shared Function ToBinString(ByVal N As Integer) As String
        Return ToBasedString(N, 2)
    End Function


    Public Shared Function ToBinString(ByVal N As UInteger) As String
        Return ToBasedString(N, 2)
    End Function


    Public Shared Function ToBinString(ByVal N As Long) As String
        Return ToBasedString(N, 2)
    End Function


    Public Shared Function ToBinString(ByVal N As ULong) As String
        Return ToBasedString(N, 2)
    End Function


    '
    ' CONVERSION FROM STRING TO NUMBER
    '
    Public Shared Function BasedToUInt64(ByVal str As String, ByVal radix As Integer) As ULong
        Dim value As ULong = 0
        Dim base As ULong = 1
        For j As Integer = 1 To str.Length
            Dim c As Char = str.Substring(str.Length - j, 1)
            Dim M = 0
            While M < radix
                If c = TNumericConverter.Alphabet(M) Then
                    value += M * base
                    Exit While
                End If
                M += 1
            End While
            If M = radix Then
                ' exception
            End If
            base *= radix
        Next
        Return value
    End Function


    ' Binary string --> Integer typed values


    Public Shared Function BinToInt16(ByVal str As String) As Short
        Return BitConverter.ToInt16(BitConverter.GetBytes(BasedToUInt64(str, 2)), 0)
    End Function

    Public Shared Function BinToUInt16(ByVal str As String) As UShort
        Return BitConverter.ToUInt16(BitConverter.GetBytes(BasedToUInt64(str, 2)), 0)
    End Function


    Public Shared Function BinToInt32(ByVal str As String) As Integer
        Return BitConverter.ToInt32(BitConverter.GetBytes(BasedToUInt64(str, 2)), 0)
    End Function


    Public Shared Function BinToUInt32(ByVal str As String) As UInteger
        Return BitConverter.ToUInt32(BitConverter.GetBytes(BasedToUInt64(str, 2)), 0)
    End Function

    Public Shared Function BinToInt64(ByVal str As String) As Long
        Return BitConverter.ToInt64(BitConverter.GetBytes(BasedToUInt64(str, 2)), 0)
    End Function


    Public Shared Function BinToUInt64(ByVal str As String) As Short
        Return BitConverter.ToInt16(BitConverter.GetBytes(BasedToUInt64(str, 2)), 0)
    End Function


    Public Shared Function HexToInt16(ByVal str As String) As Short
        Return BitConverter.ToInt16(BitConverter.GetBytes(BasedToUInt64(str, 16)), 0)
    End Function

    Public Shared Function HexToUInt16(ByVal str As String) As UShort
        Return BitConverter.ToUInt16(BitConverter.GetBytes(BasedToUInt64(str, 16)), 0)
    End Function


    Public Shared Function HexToInt32(ByVal str As String) As Integer
        Return BitConverter.ToInt32(BitConverter.GetBytes(BasedToUInt64(str, 16)), 0)
    End Function


    Public Shared Function HexToUInt32(ByVal str As String) As UInteger
        Return BitConverter.ToUInt32(BitConverter.GetBytes(BasedToUInt64(str, 16)), 0)
    End Function

    Public Shared Function HexToInt64(ByVal str As String) As Long
        Return BitConverter.ToInt64(BitConverter.GetBytes(BasedToUInt64(str, 16)), 0)
    End Function


    Public Shared Function HexToUInt64(ByVal str As String) As Short
        Return BitConverter.ToInt16(BitConverter.GetBytes(BasedToUInt64(str, 16)), 0)
    End Function


End Class

Download TNumericConverter class and sample program here.