Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Convert a hex number to binary

Posted on 1999-07-13
Medium Priority
Last Modified: 2008-03-11
What is the function to convert a hex number to binary in VB? Thanks.
Question by:John_C
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 1524464
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.

Expert Comment

ID: 1524465
ok .. I guess it's just Val("&H80000F&")  Just pass the hex number in quotes . . make sure there's a & on either end
LVL 10

Expert Comment

ID: 1524466
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Expert Comment

ID: 1524467
(Change the value of lngX to see different results)

Expert Comment

ID: 1524468
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)

Author Comment

ID: 1524469
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?
LVL 10

Accepted Solution

caraf_g earned 100 total points
ID: 1524470
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?


Author Comment

ID: 1524471
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.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question