Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert hex to ascii

Status
Not open for further replies.

p0n3g

MIS
Dec 20, 2007
2
Greetings,

I am looking for direction.

I have some xml files that contain a couple fields data that are in HEX format.

I was hoping that AWK would allow me to convert the HEX fields to ascii.

The end product would hopefully be the a new xml file with only the hex having been converted to ascii and nothing else modified.

Am I in the right place?

TIA
 
If GNU awk is available on your system you can use strtonum to make the hex strings numeric, and then printf "%c",... to convert them into characters.

Annihilannic.
 
I meant to include this example:

[tt]$ echo 0x41 | gawk '{printf "%c\n",strtonum($0)}'
A
$[/tt]

Annihilannic.
 
You may use a function like this:
Code:
function hex2dec(h      ,i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    if(!x)return "NaN"
    v=(16*v)+x-1
  }
  return v
}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV

What is this script?
How would I use it and what does it do?
 
It is an awk function. Include it at the beginning of your awk script, and call it using hex2dec(value), e.g.

[tt]hexval="0x34"
dec=hex2dec(hexval)[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top