Hex2Bin in VB

G

Guest

I was just wondering if there was some code or function that exists to
convert hex to binary in vB. My program takes a 8 bytes of hex and converts
them to binary populating each Bit in a column (64 columns). I need an easy
way instead of using more columns to perform the HEX2BIN worksheet function.

thanks
Josh
 
F

Fredrik Wahlgren

kuch68 said:
I was just wondering if there was some code or function that exists to
convert hex to binary in vB. My program takes a 8 bytes of hex and converts
them to binary populating each Bit in a column (64 columns). I need an easy
way instead of using more columns to perform the HEX2BIN worksheet function.

thanks
Josh

There is a HEX2BIN function available in the Analysis toolpak add-in.

/Fredrik
 
T

Tom Ogilvy

=Hex2bin("2b")

displays

101011

You have to have the analysis toolpak installed.

If that isn't the answer, then you need to restate your question in a
different way so we can understand it.
 
G

Guest

My mistake, what I am looking for is a conversion in my code instead of
populating columns. Something along the lines of

HexValueInput = Cells(i,j).value
BinaryRepresentation = Bin(HexValueInput)

Where Bin is a function to convert within my code. Can I use HEX2BIN there
like:

BinaryRepresentation = HEX2BIN(HexValueInput) ?

thanks again for your help
Josh
 
K

keepITcool

Kuch

try following (quick n dirty) functions.

note:
for Negative numbers the results are not similar to ATP's Dec2Bin..
results are leftpadded with 0s to multiples of 4


Option Explicit

Function IntToBin(iNumber As Integer) As String
IntToBin = HexToBin(Hex(iNumber))
End Function
Function LngToBin(lNumber As Long) As String
LngToBin = HexToBin(Hex(lNumber))
End Function

Private Function HexToBin(ByVal sHex As String) As String
Dim i&: Static col As Collection
If col Is Nothing Then
Set col = New Collection
col.Add "0000", "0"
col.Add "0001", "1"
col.Add "0010", "2"
col.Add "0011", "3"
col.Add "0100", "4"
col.Add "0101", "5"
col.Add "0110", "6"
col.Add "0111", "7"
col.Add "1000", "8"
col.Add "1001", "9"
col.Add "1010", "A"
col.Add "1011", "B"
col.Add "1100", "C"
col.Add "1101", "D"
col.Add "1110", "E"
col.Add "1111", "F"
End If
For i = Len(sHex) To 1 Step -1
HexToBin = col(Mid(sHex, i, 1)) & HexToBin
Next
End Function






--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


kuch68 wrote :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top