WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Convert Hex to Ascii

 Keywords: Convert Hex Ascii 

Question:

Does anyone have a hex to ascii routine?

I have muddled around alittle but haven't been able to get anything good working...

It starts off with: input=AskLine("", "Enter Something To Be Converted", "48656c6c6f20576f726c64") length=strlen(input) If length Mod 2 <> 0 Message("", "Invalid Hex Values") Exit EndIf

Answer:

Try this:
#DefineFunction udfHexToByte (hexstr)
hexstr = StrUpper(StrTrim(hexstr))
n1 = Char2Num(StrSub(hexstr,1,1))-48
n2 = Char2Num(StrSub(hexstr,2,1))-48
Return (((n1-7*(n1>9))<<4)+(n2-7*(n2>9)))
; note: must be StrLen(hexstr)=2
#EndFunction


sInput = "48656c6c6f20576f726c64"

iLength = StrLen(sInput)
If ((iLength mod 2) <> 0)
   Message("", "Invalid Hex Values")
Exit
EndIf

sAscii = ""
For i=1 To iLength By 2
   sHex = StrSub(sInput,i,2)
   iByte = udfHexToByte (sHex)
   sAscii = StrCat(sAscii,Num2Char(iByte))
Next

Message(sInput,sAscii)
Exit

Article ID:   W15964
File Created: 2004:03:30:15:42:04
Last Updated: 2004:03:30:15:42:04