hex to ascii

1. Junior Member
Join Date
Dec 1969
Posts
7

## hex to ascii

how do i convert a hex string back to ascii in asp?<BR><BR>thanks

2. Senior Member
Join Date
Dec 1969
Posts
404

## RE: hex to ascii

Oh, heck, it was a slow morning. There may be some function which does this that I am not aware of. But:<BR><BR><BR>sHex = hexnumber<BR><BR>If Left(sHex, 1) = "&" Then sHex = Mid(sHex,2)<BR>If Left(sHex, 1) = "H" Then sHex = Mid(sHex,2)<BR><BR>Const curBase = 16<BR>base10Num = 0<BR>power = -1<BR>For j = Len(sHex) To 1 Step -1<BR> power = power + 1<BR> multiplier = curBase ^ power<BR> thisChar = Mid(sHex,j,1)<BR> Select Case thisChar<BR> Case "A"<BR> thisCharNum = 10<BR> Case "B"<BR> thisCharNum = 11 <BR> Case "C"<BR> thisCharNum = 12<BR> Case "D"<BR> thisCharNum = 13<BR> Case "E"<BR> thisCharNum = 14<BR> Case "F"<BR> thisCharNum = 15<BR> Case Else<BR> thisCharNum = thisChar<BR> End Select<BR> base10Num = base10Num + thisCharNum * multiplier<BR>Next<BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## You will *KICK* yourself!

&nbsp;<BR>&#060;%<BR>num = CLng( "&H" & strHexNumber )<BR>%&#062;<BR><BR>

4. Junior Member
Join Date
Dec 1969
Posts
7

## RE: You will *KICK* yourself!

thanks alot.

5. Senior Member
Join Date
Dec 1969
Posts
404

## RE: You will *KICK* yourself!

Didn&#039;t know that. But, I had fun. Seemed like an interesting little challenge.

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## The "traditional" ways to do that:

&nbsp;<BR>CONST hx = "0123456789ABCDEF"<BR>num = 0<BR><BR>strHex = "FA9013"<BR><BR>For i = 1 To Len(strHex)<BR>&nbsp; &nbsp; num = num * 16 + ( InStr(hx,Mid(strHex,i,1),vbTextCompare) - 1 )<BR>Next<BR><BR>***************<BR><BR>It has the advantage that, but extending the "hx" string, you can handle most any base numbering system.<BR><BR>C programmers, though, tend to get specific and utilize the ASCII values closer. Something kind of equivalent to this:<BR><BR>num = 0<BR>For i = 1 To Len(strHex)<BR>&nbsp; &nbsp; c = Asc( Mid(strHex,i,1) )<BR>&nbsp; &nbsp; If c &#062; Asc("9") Then c = c - 7<BR>&nbsp; &nbsp; num = num * 16 + ( c AND &H0F )<BR>Next<BR>

7. Senior Member
Join Date
Dec 1969
Posts
404

## I will buy a Havana

for the person who thought up that approach. I may have come up with that solution on my 37th attempt if someone were standing over me with a whip shouting, "Do it again, differently!"! (Hope noobody gets hot and bothered with this image. Oh well, go ahead, just don&#039;t tell me about it.) (BTW, I know you were doing this quickly, but you left out the "begin at" argument in the InStr function.) <BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•