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 base16 to integer 2

Status
Not open for further replies.

AlbertAguirre

Programmer
Nov 21, 2001
273
US
How can i do this with awk?
 
You may use this function:
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.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
In GNU awk the strtonum() function will do the conversion for you if the string is prefixed by a "0x".

[tt]$ awk 'BEGIN {x="0xa"; print strtonum(x)}'
10
$[/tt]

Annihilannic.
 
GNU awk also knows the 0xN syntax, i.e. no quotes.

$ awk 'BEGIN{printf("%d\n", 0x13)}'
19
$ awk 'BEGIN{printf("%d\n", "0x13")}'
0
$ awk 'BEGIN{printf("%d\n", strtonum("0x13"))}'
19

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top