# VBForums CodeBank > CodeBank - Visual Basic .NET >  VB.NET - Number Conversion

## x-ice

*Convert Decimal - Binary, Decimal - Octal and Decimal - Hexadecimal*

To call the function/s you would do: 
VB Code:
Dim NumConv As New clsNumConv()
'Convert decimal to Binary
txtNum.Text = NumConv.Dec2Bin(CInt(txtNum.Text), True)
'Convert decimal to Octal
txtNum.Text = NumConv.Dec2Oct(CInt(txtNum.Text), True)
'Convert decimal to Hex
txtNum.Text = NumConv.Dec2Hex(CInt(txtNum.Text), True)
*Decimal to Binary*
VB Code:
Option Explicit On
Option Strict On
 Imports System.Math
 Public Class clsNumConv
    Public Function Dec2Bin(ByVal DecNum As Integer, ByVal ShowDecNum As Boolean) As String
        Dim DivNum As Integer
        'Establish how many divisions will be done, +1 for number rounding
        DivNum = CInt(Log(DecNum) / (Log(2)) + 1)
        'Create array with number of columns needed according to 'DivNum'
        Dim BinNums(DivNum, 2) As Integer
        'Assign 'DecNum' to column 1, row 0 of array
        BinNums(0, 1) = CInt(DecNum)
        Dim RowNum As Integer = 1 'Divisions loop counter
        Dim RevBinNum As String
        'Calculate divisions
        For RowNum = 1 To UBound(BinNums) 'To highest subscript of array
            'Divide previous value by 2
            BinNums(RowNum, 1) = CInt(BinNums(RowNum - 1, 1) \ 2)
            'Calculate modulus
            BinNums(RowNum, 2) = CInt(BinNums(RowNum - 1, 1) Mod 2)
            'Make binary number string
            RevBinNum &= CStr(BinNums(RowNum, 2))
        Next
        Dim FinalBinNum As String
        'Assign string to 'RevBinNum'" (reverse the string, as the binary number is backwards)
        FinalBinNum = StrReverse(RevBinNum)
        'Remove leading zeros
        FinalBinNum = FinalBinNum.TrimStart("0"c)
        'Check "ShowDecNum" to establish whether or not to show original number
        If ShowDecNum = True Then
            'Output binary number, along with original decimal number
            Return DecNum & "(10)" & " = " & FinalBinNum & "(2)"
        Else
            'Output binary number
            Return FinalBinNum & "(2)"
        End If
    End Function

----------


## x-ice

*Decimal to Octal*
VB Code:
Public Function Dec2Oct(ByVal DecNum As Integer, ByVal ShowDecNum As Boolean) As String
        Dim DivNum As Integer
        'Establish how many divisions will be done, +1 for number rounding
        DivNum = CInt(Log(DecNum) / (Log(8)) + 1)
        'Create array with number of columns needed according to 'DivNum'
        Dim OctNums(DivNum, 2) As Integer
        'Assign 'DecNum' to column 1, row 0 of array
        OctNums(0, 1) = CInt(DecNum)
        Dim RowNum As Integer = 1 'Divisions loop counter
        Dim RevOctNum As String
        'Calculate divisions
        For RowNum = 1 To UBound(OctNums) 'To highest subscript of array
            'Divide previous value by 8
            OctNums(RowNum, 1) = CInt(OctNums(RowNum - 1, 1) \ 8)
            'Calculate Modulus
            OctNums(RowNum, 2) = CInt(OctNums(RowNum - 1, 1) Mod 8)
            'Make octal number string
            RevOctNum &= CStr(OctNums(RowNum, 2))
        Next
        Dim FinalOctNum As String
        'Assign string to 'RevOctNum'" (reverse the string, as the octal number is backwards)
        FinalOctNum = StrReverse(RevOctNum)
        'Remove leading zeros
        FinalOctNum = FinalOctNum.TrimStart("0"c)
        'Check "ShowDecNum" to establish whether or not to show original number
        If ShowDecNum = True Then
            'Output octal number, along with original decimal number
            Return DecNum & "(10)" & " = " & FinalOctNum & "(8)"
        Else
            'Output octal number
            Return FinalOctNum & "(8)"
        End If
    End Function

----------


## x-ice

*Decimal to Hexadecimal*
VB Code:
Public Function Dec2Hex(ByVal DecNum As Integer, ByVal ShowDecNum As Boolean) As String
        Dim DivNum As Integer
        'Establish how many divisions will be done, +1 for number rounding
        DivNum = CInt(Log(DecNum) / (Log(16)) + 1)
        'Create array with number of columns needed according to 'DivNum'
        Dim HexNums(DivNum, 2) As String 'Must be string to hold hex values
        'Assign 'DecNum' to column 1, row 0 of array
        HexNums(0, 1) = CStr(DecNum)
        Dim RowNum As Integer = 1 'Divisions loop counter
        Dim RevHexNum As String
        'Calculate divisions
        For RowNum = 1 To UBound(HexNums) 'To highest subscript of array
            'Divide previous value by 16
            HexNums(RowNum, 1) = CStr(CInt(HexNums(RowNum - 1, 1)) \ 16)
            Dim modValue As Integer
            'Calculate Modulus
            modValue = CInt(HexNums(RowNum - 1, 1)) Mod 16
            'Convert numbers above 9 to hex A-F
            Select Case modValue
                Case 10
                    'Convert number to hex A
                    HexNums(RowNum, 2) = "A"
                Case 11
                    'Convert number to hex B
                    HexNums(RowNum, 2) = "B"
                Case 12
                    'Convert number to hex C
                    HexNums(RowNum, 2) = "C"
                Case 13
                    'Convert number to hex D
                    HexNums(RowNum, 2) = "D"
                Case 14
                    'Convert number to hex E
                    HexNums(RowNum, 2) = "E"
                Case 15
                    'Convert number to hex F
                    HexNums(RowNum, 2) = "F"
                Case Else
                    'Number is not above 9, so use original number
                    HexNums(RowNum, 2) = CStr(modValue)
            End Select
            'Make hex number string
            RevHexNum &= CStr(HexNums(RowNum, 2))
        Next
        Dim FinalHexNum As String
        'Assign string to 'RevHexNum'" (reverse the string, as the hex number is backwards)
        FinalHexNum = StrReverse(RevHexNum)
        'Remove leading zeros
        FinalHexNum = FinalHexNum.TrimStart("0"c)
        'Check "ShowDecNum" to establish whether or not to show original number
        If ShowDecNum = True Then
            'Output hex number, along with original decimal number
            Return DecNum & "(10)" & " = " & FinalHexNum & "(16)"
        Else
            'Output hex number
            Return FinalHexNum & "(16)"
        End If
    End Function
End Class

----------


## jmcilhinney

Note that

Math.Floor(int1 / int2)

is the same as

int1 \ int2

----------


## x-ice

> Note that
> 
> Math.Floor(int1 / int2)
> 
> is the same as
> 
> int1 \ int2


Ok, sorted it.

----------


## jmcilhinney

This will work for all bases up to 16 and for negative numbers:
VB Code:
Public Function DecToBin(ByVal decNum As Integer) As String
        Return DecToBaseX(decNum, 2)
    End Function
     Public Function DecToOct(ByVal decNum As Integer) As String
        Return DecToBaseX(decNum, 8)
    End Function
     Public Function DecToHex(ByVal decNum As Integer) As String
        Return DecToBaseX(decNum, 16)
    End Function
     Public Function DecToBaseX(ByVal decNum As Integer, ByVal base As Integer) As String
        Dim isNegative As Boolean = decNum < 0
        Dim newBaseNum As String = String.Empty
        Dim modVal As Integer
         decNum = Math.Abs(decNum)
         Do
            modVal = decNum Mod base
             Select Case modVal
                Case 15
                    newBaseNum = "F" & newBaseNum
                Case 14
                    newBaseNum = "E" & newBaseNum
                Case 13
                    newBaseNum = "D" & newBaseNum
                Case 12
                    newBaseNum = "C" & newBaseNum
                Case 11
                    newBaseNum = "B" & newBaseNum
                Case 10
                    newBaseNum = "A" & newBaseNum
                Case Else
                    newBaseNum = modVal.ToString() & newBaseNum
            End Select
             decNum = decNum \ base
        Loop While decNum > 0
         If isNegative Then
            newBaseNum = "-" & newBaseNum
        End If
         Return newBaseNum
    End Function

----------


## jmcilhinney

Just had a look and the Convert.ToString(Int32, Int32) method also provides this functionality.

----------

