Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hex string to decimal number

2,974 views
Skip to first unread message

Duncan Square

unread,
Jan 21, 1997, 3:00:00 AM1/21/97
to

Im trying to convert a hexadecimal string into a decimal number using awk,
and not succeeding.

e.g.

echo ff1ac5ff | awk '{printf "%d\n", $1}'

(which should print the decimal of ff1ac5ff)

From playing around, it appears that i also have to split the hexadecimal
number into groups of four. This must be a common problem, but i have'nt
seen anything published in any faq's and general awk help www pages.

thanks

--

Bill Marcum

unread,
Jan 21, 1997, 3:00:00 AM1/21/97
to

In article <slrn5ea4l...@kedemel.demon.co.uk>, Duncan Square wrote:
>
>Im trying to convert a hexadecimal string into a decimal number using awk,
>and not succeeding.
>
The printf command in awk can convert a decimal number to hex, octal, or
ascii, but it can't convert to decimal. You'll have to do it the hard
way, like:
awk '{for (x=1;x<=length($1);x++){
digit=index("123456789abcdef",substr($1,x,1));
number=number*16+digit}
print number}'

or you could convert hex to decimal using another tool such as bc

>e.g.
>
>echo ff1ac5ff | awk '{printf "%d\n", $1}'
>
>(which should print the decimal of ff1ac5ff)
>
>From playing around, it appears that i also have to split the hexadecimal
>number into groups of four. This must be a common problem, but i have'nt
>seen anything published in any faq's and general awk help www pages.
>
>thanks
>
>--


--
Bill Marcum bmarcum at iglou dot com
In the computer lab, Nyarlathotep turned back to the terminal. He hit the
Key With No Name Or ASCII Equivalent, and vanished.

Dan Mercer

unread,
Jan 22, 1997, 3:00:00 AM1/22/97
to

Duncan Square (ro...@kedemel.demon.co.uk) wrote:

: Im trying to convert a hexadecimal string into a decimal number using awk,
: and not succeeding.

: e.g.

: echo ff1ac5ff | awk '{printf "%d\n", $1}'

: (which should print the decimal of ff1ac5ff)

No, it should print the same thing as:
echo string | awk '{printf "%d\n", $1}'

which is 0, which it does. If you want to convert ff1ac5ff, you'll
have to write your own function using substr.

: From playing around, it appears that i also have to split the hexadecimal


: number into groups of four. This must be a common problem, but i have'nt
: seen anything published in any faq's and general awk help www pages.

: thanks

: --

--
Dan Mercer
Reply To: dame...@mmm.com

Opinions expressed herein are my own and may not represent those of my employer.


Yuli Barcohen

unread,
Jan 22, 1997, 3:00:00 AM1/22/97
to Duncan Square

>>>>> In article <slrn5ea4l...@kedemel.demon.co.uk>,
>>>>> ro...@kedemel.demon.co.uk (Duncan Square) writes:

Duncan> Im trying to convert a hexadecimal string into a decimal
Duncan> number using awk, and not succeeding.

Duncan> e.g.

Duncan> echo ff1ac5ff | awk '{printf "%d\n", $1}'

Duncan> (which should print the decimal of ff1ac5ff)

It shouldn't. AWK doesn't perform any automatic hex to dec conversion. I
prefer to use `dc':

echo 16i FF1AC5FF pq | dc

does the job (note uppercase). `dc' also hasn't got any limits on the
number length.

Duncan> From playing around, it appears that i also have to split
Duncan> the hexadecimal number into groups of four. This must be a
Duncan> common problem, but i have'nt seen anything published in any
Duncan> faq's and general awk help www pages.

Duncan> thanks

--

d...@cts.com

unread,
Jan 24, 1997, 3:00:00 AM1/24/97
to

ro...@kedemel.demon.co.uk (Duncan Square) wrote:


>Im trying to convert a hexadecimal string into a decimal number using awk,
>and not succeeding.

>e.g.

>echo ff1ac5ff | awk '{printf "%d\n", $1}'

>(which should print the decimal of ff1ac5ff)

Just rewrite your printf statement as
printf "%d\n" (("0x" $1) + 0)

Your problem is that $1 is just a string (and one that isn't numeric)
and strings don't convert to numbers in this case. By adding "0x" to
the start of the string, your string does take the form of a
hexadecimal literal. Add 0 to the string to force it now to be
converted to a number.

I tested this under DOS and Thompson AWK (TAWK) and it works fine.

You have to be careful with very long hex inputs. TAWK can't handle a
number larger than a double-word. If you need to convert hex numbers
larger than that, perhaps you will have to use 'bc' or spend much more
time fiddling by yourself.

DKM


Patrick Karl

unread,
Jan 26, 1997, 3:00:00 AM1/26/97
to Duncan Square, karljp

Duncan Square wrote:
>
> Im trying to convert a hexadecimal string into a decimal number using awk,
> and not succeeding.
>
> e.g., echo ff1ac5ff | awk '{printf "%d\n", $1}'

Well, here's my solution. Unfortunately, it doesn't work with the
original
awk (e.g., the "awk" on Solaris), so you will have to do:

echo ff1ac5ff | nawk -f hex2dec.awk

or

echo ff1ac5ff | gawk -f hex2dec.awk

where hex2dec.awk is:

BEGIN { for(i=0;i<10;i++) decv[i]=i;
decv["a"]=10;decv["b"]=11;decv["c"]=12;decv["d"]=13;
decv["e"]=14;decv["f"]=15;
decv["A"]=10;decv["B"]=11;decv["C"]=12;decv["D"]=13;
decv["E"]=14;decv["F"]=15
}
function hex(x) {
value = 0;
i = 1;
n=length(x);
while ( n > 0 ) {
value = 16*value + decv[substr(x,i,1)];
n--;
i++;
}
return value
}

{printf("%12.0f\n",hex($1))}

BTW, I get: 4279944703 , when I do "echo ff1ac5ff | nawk -f
hex2dec.awk"
under Solaris 5.5.1.

BTW, this is an implementation of what I would call the "classical"
method of
converting a hexadecimal string to a decimal string.

0 new messages