Converting hex to ascii

T

Tom

How do I convert a hex byte into an ascii character? I'll take an
excel formula or VB code. Either way would be fine. I could not find
an excel function to do this. I appreciate any help submitted. Thank
you.

tom
 
R

Rob van Gelder

It's confusing to say "hex byte" because that's mixing presentation with
storage. A Byte is a Byte and can be presented as decimal, hexadecimal,
binary, etc...

So, by Hex Byte I assume you actually mean hex string.

The first is Byte to Ascii, the secon is HexString to Ascii.

Sub test()
Dim byt As Byte, str As String

byt = &H41 'A
Debug.Print Chr(byt)

str = "41"
Debug.Print Chr("&H" & str)
End Sub
 
G

Gerrit van Stempvoort

Hello,

To be able to work with hex numbers you have to install the Add-In "Analysis
Toolpack" first (Tools Menu, Add-Ins and check the Analysis Toolpack
checkbox).
You may need the Office Installation CD to do this.
When this is done you find a new function category (Insert Function) with
the name "Engineering". In this function category you fine several functions
like Hex2Dec.

In VB it seems to be more complicated.
The sub:

Dim Dec1 As Long

Dec1 = "&hF"

End Sub

will return the value 15 for Dec1, but if you use a variable input like in
this sub:

Dim Hex1 As String
Dim Dec1 As Long

Sub HexTest()

Hex1 = "F"
Dec1 = "&h(Hex1)"

End Sub

Then I get a "type mismatch" error.

So if someone can help me how to avoid this problem I wil be very grateful.

Thanks beforehand,

Gerrit


In EXCEL you have to install the
 
S

Steve Garman

Two possibilities:

Sub HexTest()
Dim hex1 As String
Dim dec1 As Integer
hex1 = "F"
dec1 = "&h" & hex1
MsgBox dec1
hex1 = "ff"
MsgBox hx2dc(hex1)
End Sub

Function hx2dc(s$) As Long
hx2dc = "&h" & s$
End Function
 
T

Tom

Thanks for your help Rob. I guess I had an older version of VB help
because I could not find any leads to the chr() function.

tom
 

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