Luciano Evaristo Guerche

A brazilian geek interested in .NET technologies

About Me

Bin, Oct and Hex

Visual Basic 6 has the Hex function, but no Bin or Oct, so I wrote the silly functions bellow, just to have a bit of fun.
Option Explicit

Public Function Bin(ByVal lngNumber As Long) As String
    Do While lngNumber > 0
        Bin = (lngNumber Mod 2) & Bin
        lngNumber = lngNumber \ 2
    Loop
    If (Len(Bin) Mod 8) <> 0 Then
        Bin = String$(8 - (Len(Bin) Mod 8), "0") & Bin
    End If
End Function

Public Function Oct(ByVal lngNumber As Long) As String
    Do While lngNumber > 0
        Oct = (lngNumber Mod 8) & Oct
        lngNumber = lngNumber \ 8
    Loop
    If (Len(Oct) Mod 4) <> 0 Then
        Oct = String$(4 - (Len(Oct) Mod 4), "0") & Oct
    End If
End Function

Comments

Jon Galloway said:

For hex conversions, you can use the int.ToString("x") method:

(16).ToString("x") returns "10"
# October 1, 2004 2:34 PM

Jon Galloway said:

For octal:
string oct = Convert.ToString(intValue, 8);
# October 1, 2004 2:39 PM

Jon Galloway said:

Oops, pardon me - I didn't notice this was VB6. Sorry about that. You can delete my comments if you'd like.
# October 1, 2004 2:40 PM

Luciano Evaristo Guerche said:

Jon,

Do not worry about and feel free to add more comments if you want. Thanks for letting me know vb .net has this options embedded. Another thing I have noted after I posted, is that vb6 Hex function works with negative numbers, mine Bin and Oct functions don't, but it was just for fun, so I won't brainstorm to have them working with negatives.
# October 1, 2004 3:03 PM

edween said:

how about any bin/oct/hex convet to decimal... is it possible???
sorry dont know about vb.. just askin for help..
# October 9, 2004 11:47 AM

Luciano Evaristo Guerche said:

Edween,

Option Explicit

Public Function ToDecimal(strNumber As String, intBaseNumber As Integer) As Double
Dim intCurrentDigit As Integer
Dim strValidDigits As String
For intCurrentDigit = 0 To intBaseNumber - 1
If intCurrentDigit < 10 Then
strValidDigits = strValidDigits & intCurrentDigit
Else
strValidDigits = strValidDigits & Chr$((Asc("A") - 10) + intCurrentDigit) & Chr$((Asc("a") - 10) + intCurrentDigit)
End If
Next
strValidDigits = "[" & strValidDigits & "]"
For intCurrentDigit = Len(strNumber) To 1 Step -1
If Not Mid$(strNumber, intCurrentDigit, 1) Like strValidDigits Then
Err.Raise vbObjectError + 1, "ToDecimal", "Invalid number"
End If
Next
For intCurrentDigit = Len(strNumber) To 1 Step -1
If Mid$(strNumber, intCurrentDigit, 1) <> "0" Then
If Mid$(strNumber, intCurrentDigit, 1) Like "[A-Z]" Then
ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc("A") + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))
ElseIf Mid$(strNumber, intCurrentDigit, 1) Like "[a-z]" Then
ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc("a") + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))
Else
ToDecimal = ToDecimal + (Mid$(strNumber, intCurrentDigit, 1) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))
End If
End If
Next
End Function
# October 10, 2004 11:05 AM

Luciano Evaristo Guerche said:

Edween,

Hope this is the code you asked for:

<pre>
Option Explicit

Public Function ToDecimal(strNumber As String, intBaseNumber As Integer) As Double
Dim intCurrentDigit As Integer
Dim strValidDigits As String
For intCurrentDigit = 0 To intBaseNumber - 1
If intCurrentDigit < 10 Then
strValidDigits = strValidDigits & intCurrentDigit
Else
strValidDigits = strValidDigits & Chr$((Asc("A") - 10) + intCurrentDigit) & Chr$((Asc("a") - 10) + intCurrentDigit)
End If
Next
strValidDigits = "[" & strValidDigits & "]"
For intCurrentDigit = Len(strNumber) To 1 Step -1
If Not Mid$(strNumber, intCurrentDigit, 1) Like strValidDigits Then
Err.Raise vbObjectError + 1, "ToDecimal", "Invalid number"
End If
Next
For intCurrentDigit = Len(strNumber) To 1 Step -1
If Mid$(strNumber, intCurrentDigit, 1) <> "0" Then
If Mid$(strNumber, intCurrentDigit, 1) Like "[A-Z]" Then
ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc("A") + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))
ElseIf Mid$(strNumber, intCurrentDigit, 1) Like "[a-z]" Then
ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc("a") + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))
Else
ToDecimal = ToDecimal + (Mid$(strNumber, intCurrentDigit, 1) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))
End If
End If
Next
End Function
</pre>
# October 10, 2004 11:08 AM

Luciano Evaristo Guerche said:

Edween,

This is the way you might use it:

? ToDecimal("11111111", 2)
? ToDecimal("377", 8)
? ToDecimal("FF", 16)
? ToDecimal("GG", 16) 'will raise an error
# October 10, 2004 11:10 AM

Luciano Evaristo Guerche said:

Another way of converting Decimal to Bin, Oct and Hex?

There it is:

<pre>
Option Explicit

Public Function FromDecimal(ByVal lngNumber As Long, ByVal intBaseNumber As Integer) As String
Do While lngNumber > 0
If (lngNumber Mod intBaseNumber) < 10 Then
FromDecimal = (lngNumber Mod intBaseNumber) & FromDecimal
Else
FromDecimal = Chr$(Asc("A") + (lngNumber Mod intBaseNumber) - 10) & FromDecimal
End If
lngNumber = lngNumber \ intBaseNumber
Loop
End Function
</pre>
# October 10, 2004 11:21 AM

Luciano Evaristo Guerche said:

And this is how you might use it:

? FromDecimal(255, 2)
11111111
? FromDecimal(255, 8)
377
? FromDecimal(255, 16)
FF
# October 10, 2004 11:22 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)