1820623 Members
2003 Online
109626 Solutions
New Discussion юеВ

Re: Dec to hex with awk

 
SOLVED
Go to solution
Chris Frangandonis
Regular Advisor

Dec to hex with awk

Hi All,

I can convert from hex to dec but I seem to get lost from dec to hex within "AWK". Harry once showed me the hex to dec.

Any Ideas

Many Thanks
Chris
7 REPLIES 7
Chris Wilshaw
Honored Contributor

Re: Dec to hex with awk

You need the printf %x notification;

eg:

echo 10 | awk '{printf("%x\n",$1)}'
a

echo 11 | awk '{printf("%x\n",$1)}'
b

echo 244 | awk '{printf("%x\n",$1)}'
f4
Chris Frangandonis
Regular Advisor

Re: Dec to hex with awk

Hi Chris,

Thanks

What how about if I got it in a function and need to define it so as to printf it. e.g


###########################
function get_info(file)
###########################

{
e_b="LookFor"
SEA="*"$6"."$7"
{if (match($7,"swing")){
TOOL="/usr/bin/which CheckFor "
TOOL | getline TOOL2;close(TOOL)
#this is where I need to substr and the convert to Dec

Once Again Thanks
Chris
Sundar_7
Honored Contributor

Re: Dec to hex with awk

Chris,

You can also do with adb.

echo "0D=X" | adb

-- Sundar.

Learn What to do ,How to do and more importantly When to do ?
Hein van den Heuvel
Honored Contributor

Re: Dec to hex with awk


Within an awk program/script just use 'sprintf' to print to a string:


awk 'BEGIN{x=sprintf("%0X",30);print x,x,x}'

Hein.
Chris Frangandonis
Regular Advisor

Re: Dec to hex with awk

Hi Hein,

Thanks that was great,(rusting). What if I have a date 20040818040320

YER=sprintf("%s",(substr($1,3,2)))
MON=sprintf("%0x",(substr($1,5,2)))
DAY=sprintf("%0x",(substr($1,7,2))) (this will be 18 = 12
HH=sprintf("%0x",(substr($2,7,2))) 04 = 4
etc
I would like to define it a the end e.g
YMDHMS= YERMONDAYHH so as to used it for a search pattern.
In the hour (04), converting it to dec will be 4 (048124) which is what I am not looking for. What I would like the result to be "040812040332"

Any Ideas

Thanks Again
Chris
Hein van den Heuvel
Honored Contributor
Solution

Re: Dec to hex with awk


Now you confused me on when you are goign from hex to dec or back. Still, never mind, the basic formatting principle apply:
For example "%02X"
% = format coming up!
0 = leading 0 requestion
2 = fieldsize 2
X = upcased hexadecimal (vs x for lower, and d for decimal)

And you can combine lots of formats, with lots of (prefereably as many :-) input in a single sprintf giving something like:

echo "20040818040320" | awk '{x=sprintf("%s%02X%02X%02d",substr($1,3,2),substr($1,5,2),substr($1,7,2),substr($1,9,2));print x}'


You'll have to verify spaces and X and d usage.

Hein.
Chris Frangandonis
Regular Advisor

Re: Dec to hex with awk

Hein,

Great explanation with 10. Its all coming back to me.

Anyway Thanks to all role players.

Chris