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

10 Comments

  • For hex conversions, you can use the int.ToString(&quot;x&quot;) method:



    (16).ToString(&quot;x&quot;) returns &quot;10&quot;

  • For octal:

    string oct = Convert.ToString(intValue, 8);

  • Oops, pardon me - I didn't notice this was VB6. Sorry about that. You can delete my comments if you'd like.

  • 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.

  • how about any bin/oct/hex convet to decimal... is it possible???

    sorry dont know about vb.. just askin for help..

  • 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 &lt; 10 Then

    strValidDigits = strValidDigits &amp; intCurrentDigit

    Else

    strValidDigits = strValidDigits &amp; Chr$((Asc(&quot;A&quot;) - 10) + intCurrentDigit) &amp; Chr$((Asc(&quot;a&quot;) - 10) + intCurrentDigit)

    End If

    Next

    strValidDigits = &quot;[&quot; &amp; strValidDigits &amp; &quot;]&quot;

    For intCurrentDigit = Len(strNumber) To 1 Step -1

    If Not Mid$(strNumber, intCurrentDigit, 1) Like strValidDigits Then

    Err.Raise vbObjectError + 1, &quot;ToDecimal&quot;, &quot;Invalid number&quot;

    End If

    Next

    For intCurrentDigit = Len(strNumber) To 1 Step -1

    If Mid$(strNumber, intCurrentDigit, 1) &lt;&gt; &quot;0&quot; Then

    If Mid$(strNumber, intCurrentDigit, 1) Like &quot;[A-Z]&quot; Then

    ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc(&quot;A&quot;) + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))

    ElseIf Mid$(strNumber, intCurrentDigit, 1) Like &quot;[a-z]&quot; Then

    ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc(&quot;a&quot;) + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))

    Else

    ToDecimal = ToDecimal + (Mid$(strNumber, intCurrentDigit, 1) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))

    End If

    End If

    Next

    End Function

  • Edween,



    Hope this is the code you asked for:



    &lt;pre&gt;

    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 &lt; 10 Then

    strValidDigits = strValidDigits &amp; intCurrentDigit

    Else

    strValidDigits = strValidDigits &amp; Chr$((Asc(&quot;A&quot;) - 10) + intCurrentDigit) &amp; Chr$((Asc(&quot;a&quot;) - 10) + intCurrentDigit)

    End If

    Next

    strValidDigits = &quot;[&quot; &amp; strValidDigits &amp; &quot;]&quot;

    For intCurrentDigit = Len(strNumber) To 1 Step -1

    If Not Mid$(strNumber, intCurrentDigit, 1) Like strValidDigits Then

    Err.Raise vbObjectError + 1, &quot;ToDecimal&quot;, &quot;Invalid number&quot;

    End If

    Next

    For intCurrentDigit = Len(strNumber) To 1 Step -1

    If Mid$(strNumber, intCurrentDigit, 1) &lt;&gt; &quot;0&quot; Then

    If Mid$(strNumber, intCurrentDigit, 1) Like &quot;[A-Z]&quot; Then

    ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc(&quot;A&quot;) + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))

    ElseIf Mid$(strNumber, intCurrentDigit, 1) Like &quot;[a-z]&quot; Then

    ToDecimal = ToDecimal + ((Asc(Mid$(strNumber, intCurrentDigit, 1)) - Asc(&quot;a&quot;) + 10) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))

    Else

    ToDecimal = ToDecimal + (Mid$(strNumber, intCurrentDigit, 1) * intBaseNumber ^ (Len(strNumber) - intCurrentDigit))

    End If

    End If

    Next

    End Function

    &lt;/pre&gt;

  • Edween,



    This is the way you might use it:



    ? ToDecimal(&quot;11111111&quot;, 2)

    ? ToDecimal(&quot;377&quot;, 8)

    ? ToDecimal(&quot;FF&quot;, 16)

    ? ToDecimal(&quot;GG&quot;, 16) 'will raise an error

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



    There it is:



    &lt;pre&gt;

    Option Explicit



    Public Function FromDecimal(ByVal lngNumber As Long, ByVal intBaseNumber As Integer) As String

    Do While lngNumber &gt; 0

    If (lngNumber Mod intBaseNumber) &lt; 10 Then

    FromDecimal = (lngNumber Mod intBaseNumber) &amp; FromDecimal

    Else

    FromDecimal = Chr$(Asc(&quot;A&quot;) + (lngNumber Mod intBaseNumber) - 10) &amp; FromDecimal

    End If

    lngNumber = lngNumber \ intBaseNumber

    Loop

    End Function

    &lt;/pre&gt;

  • And this is how you might use it:



    ? FromDecimal(255, 2)

    11111111

    ? FromDecimal(255, 8)

    377

    ? FromDecimal(255, 16)

    FF

Comments have been disabled for this content.