Thread: Hex2Bin in VB
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
keepITcool keepITcool is offline
external usenet poster
 
Posts: 2,253
Default Hex2Bin in VB

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 :

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




"Tom Ogilvy" wrote:

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

--
Regards,
Tom Ogilvy

"kuch68" wrote in message
...
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