Convert a hex number to binary

What is the function to convert a hex number to binary in VB? Thanks.
Who is Participating?
caraf_gConnect With a Mentor Commented:
Hello John_C

Internally there is only one format, the binary format. I can only assume that you are well aware of this, so you are looking for a string representation of the binary data.

Alas, VB does not have any in-built formats for displaying data in binary or hexadecimal formats. It allows you to input values as hexadecimal values alright:

Dim lngX As Long

is the same as
lngX = -1

And as BeedleGuis has pointed out, you can allow a user to enter hexadecimal data in, for example a text box, and then interpret the string as a hexadecimal number:

lngX = Val("&H" & Text1.Text1) for example, will allow the user to simply type in a string to represent a hexadecimal value, such as "AB01D"

There is a "Hex" function in VB that allows you to represent a number as a hexadecimal string. Alas, the function is a bit temperamental. But you can use it to represent it as a "binary" string.

Once you've got the hex string, simply replace the following
0 -> 0000
1 -> 0001
2 -> 0010
F -> 1111

lngX = <value>
strY = hex(lngX)
'Do above replacement to strY

VB has no built-in function to represent values as binary strings, so you will have to write something yourself. To save some extra processing time you can use Byte arrays instead of strings, but it's never going to be whiz-bang fast.

Ok back to hex, I said it was temperamental. Actually, it isn't. It's the &H operator that is temperamental...

If you do an assignment
Dim lngX As Long
lngX = &H<some characters here>
you'll get into trouble if there are exactly 4 characters:

You're OK as long as you stay under 8000:

lngX = &H7FFF
msgbox lngX 'correctly shows 32767

But anything over it:
lngX = &8000 ' incorrectly shows -32768

Why is this - because if you specify exactly 4 characters, the VB interpreter incorrectly interprets the value as an Integer, not as a Long. The Long value &H8000 is 32768, but the Integer value &H8000 is the largest possible negative Integer, i.e. -32768.

This erratic behaviour continues up to &HFFFF = -1, and then, suddenly
&H100000 is a positive number again, because now it MUST interpret it as a Long.

Good, eh?

val() will return the binary format, but you need to pass the hex number a special way.  I'll have to look it up and get back to you.
ok .. I guess it's just Val("&H80000F&")  Just pass the hex number in quotes . . make sure there's a & on either end
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Private Sub Command1_Click()

Dim lngX As Long

Dim lngY As Long

Dim strBinary As String


lngY = 1
On Error Resume Next
strBinary = ""
Do While Err.Number = 0
    If lngX And lngY Then
        strBinary = "1" & strBinary
        strBinary = "0" & strBinary
    End If
    lngY = lngY + lngY

If lngX And &H80000000 Then
    strBinary = "1" & strBinary
    strBinary = "0" & strBinary
End If

MsgBox strBinary

End Sub

(Change the value of lngX to see different results)
That's fine and dandy for doing a few but what is you are talking reasonable size files ie 100k +

I takes foreverm anyone got any quicker solutions (I know this isn't my question but hey)
John_CAuthor Commented:
Thanks everyone.

I tried Val(), but seems like it only returns in decimal format, even if I pass the number using BeedleGuis' format. May be different version? I'm using VB6.

There is no build-in function in VB?
John_CAuthor Commented:
Yes I'm aware that computer stores everything in binary format.
If the function doesn't exist I'll use a function to do it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.