# number conversions (binary, decimal, hex)

Page 1 of 1

## 2 Replies - 2672 Views - Last Post: 11 January 2011 - 12:00 AM

### #1 Liontrue1 Reputation: 48
• Posts: 811
• Joined: 20-May 06

# number conversions (binary, decimal, hex)

Posted 18 December 2006 - 03:11 PM

Description: Put these 6 functions in the same module because they are linked together.These 6 functions are used to convert a type of number to another type. The functions are: bin2dec: Binary -> Decimal bin2hex: Binary -> Hex dec2bin: Decimal ->Binary dec2hex: Decimal -> Hex hex2bin: Hex -> Binary hex2dec: Hex -> Decimal
```'''''''''''''''Number conversion functions

'Decimal -> binary
Public Function dec2bin(ByVal num As String) As String
Dim number As String
Dim step As Long
Dim bstr As String
Dim neg As Boolean
step = 1
bstr = ""
If Left(num, 1) = "-" Then
neg = True
number = Right(num, Len(num) - 1)
Else
neg = False
number = num
End If
While Left(number, 1) = "0"
number = Right(number, Len(number) - 1)
Wend
If Len(number) = 0 Then
dec2bin = 0
Exit Function
End If
While step * 2 <= number
step = step * 2
Wend
While step >= 1
If number >= step Then
number = number - step
bstr = bstr & "1"
Else
bstr = bstr & "0"
End If
step = step / 2
Wend
If neg = True Then
bstr = "-" & bstr
End If
dec2bin = bstr
End Function

'Binary -> decimal
Public Function bin2dec(ByVal bin As String) As String
Dim cont As Long
Dim bstr As String
Dim step As Long
Dim neg As Boolean
step = 1
cont = 0
If Left(bin, 1) = "-" Then
neg = True
bstr = Right(bin, Len(bin) - 1)
Else
neg = False
bstr = bin
End If
While Left(bstr, 1) = "0"
bstr = Right(bstr, Len(bstr) - 1)
Wend
If Len(bstr) = 0 Then
bin2dec = 0
Exit Function
End If
While Len(bstr) > 0
If Right(bstr, 1) = "1" Then
cont = cont + step
End If
bstr = Left(bstr, Len(bstr) - 1)
step = step * 2
Wend
If neg = True Then
cont = "-" & cont
End If
bin2dec = cont
End Function

Public Function dec2hex(ByVal num As String) As String
Dim number As String
Dim neg As Boolean
number = num
If Left(number, 1) = "-" Then
neg = True
number = Right(number, Len(number) - 1)
Else
neg = False
End If
While Left(number, 1) = "0"
number = Right(number, Len(number) - 1)
Wend
If Len(number) = 0 Then
dec2hex = 0
Exit Function
End If
number = Hex(number)
If neg = True Then
number = "-" & number
End If
dec2hex = LCase(number)
End Function

Public Function hex2dec(ByVal num As String) As String
Dim bstr As String
Dim cont As String
Dim step As String
Dim number As String
Dim neg As Boolean
number = num
cont = ""
If Left(number, 1) = "-" Then
neg = True
number = Right(number, Len(number) - 1)
Else
neg = False
End If
While Left(number, 1) = "0"
number = Right(number, Len(number) - 1)
Wend
If Len(number) = 0 Then
hex2dec = 0
Exit Function
End If
While Len(number) > 0
step = Left(number, 1)
bstr = hex2bin(step)
While Len(bstr) < 4
bstr = "0" & bstr
Wend
cont = cont & bstr
number = Right(number, Len(number) - 1)
Wend
If neg = True Then
cont = "-" & cont
End If
hex2dec = bin2dec(cont)
End Function

Public Function hex2bin(ByVal num As String) As String
Dim bstr As String
Dim cont As String
Dim step As String
Dim number As String
Dim neg As Boolean
Dim dnum As String
number = num
cont = ""
step = ""
dnum = ""
bstr = ""
If Left(number, 1) = "-" Then
neg = True
number = Right(number, Len(number) - 1)
Else
neg = False
End If
While Left(number, 1) = "0"
number = Right(number, Len(number) - 1)
Wend
If Len(number) = 0 Then
hex2bin = 0
Exit Function
End If
While Len(number) > 0
step = Left(number, 1)
Select Case step
Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
dnum = step
Case "a":
dnum = 10
Case "b":
dnum = 11
Case "c":
dnum = 12
Case "d":
dnum = 13
Case "e":
dnum = 14
Case "f":
dnum = 15
End Select
cont = dec2bin(dnum)
While Len(cont) < 4
cont = "0" & cont
Wend
bstr = bstr & cont
number = Right(number, Len(number) - 1)
Wend
While Left(bstr, 1) = "0"
bstr = Right(bstr, Len(bstr) - 1)
Wend
If neg = True Then
bstr = "-" & bstr
End If
hex2bin = bstr
End Function

Public Function bin2hex(ByVal num As String) As String
Dim chars(15) As String
Dim cont As String
Dim step As String
Dim number As String
Dim neg As Boolean
chars(0) = "0"
chars(1) = "1"
chars(2) = "2"
chars(3) = "3"
chars(4) = "4"
chars(5) = "5"
chars(6) = "6"
chars(7) = "7"
chars(8) = "8"
chars(9) = "9"
chars(10) = "a"
chars(11) = "b"
chars(12) = "c"
chars(13) = "d"
chars(14) = "e"
chars(15) = "f"
number = num
cont = ""
step = ""
If Left(number, 1) = "-" Then
neg = True
number = Right(number, Len(number) - 1)
Else
neg = False
End If
While Left(number, 1) = "0"
number = Right(number, Len(number) - 1)
Wend
If Len(number) = 0 Then
bin2hex = 0
Exit Function
End If
While Len(number) Mod 4 <> 0
number = "0" + number
Wend
While Len(number) >= 4
step = Left(number, 4)
cont = cont + chars(bin2dec(step))
number = Right(number, Len(number) - 4)
Wend
If neg = True Then
cont = "-" & cont
End If
bin2hex = cont
End Function

```

Is This A Good Question/Topic? 0

## Replies To: number conversions (binary, decimal, hex)

### #2 anerao20 Reputation: 0
• Posts: 0
• Joined: 11-October 09

## Re: number conversions (binary, decimal, hex)

Posted 11 October 2009 - 01:56 AM

what is the use of "step"???

### #3 jecho Reputation: 0
• Posts: 1
• Joined: 11-January 11

## Re: number conversions (binary, decimal, hex)

Posted 11 January 2011 - 12:00 AM

there is an error..you must check your language varaibles

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }