Operating System - Linux
1756729 Members
2750 Online
108852 Solutions
New Discussion юеВ

convert hexa to decimal values

 
SOLVED
Go to solution
mobidyc
Trusted Contributor

convert hexa to decimal values

Hello,

i'm trying to convert an hexadicimal value to decimal but it seems impossible with my knowledge. ;)

that's what i tried (under ksh).
HPUX B.11.00:
# echo 0xfffff000=D |adb
-4096
# printf "%d\n" 0xfffff000
printf: Error converting 0xfffff000
2147483647
# print - $((16#fffff000))
-4096
# echo "obase=86 0xfffff000" |bc -l
syntax error on line 1, /usr/lib/lib.b
# echo "obase=86 ; fffff000" |bc
syntax error on line 1
# echo "fffff000" |tr "[[:xdigit:]]" "[[:digit:]]"
tr: String2 contains an invalid character class.

i've the same errors in HP-UX B.11.11/B.11.23

i need to convert these values for a script so, do you have advice for it ?

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
12 REPLIES 12
Dennis Handly
Acclaimed Contributor

Re: convert hexa to decimal values

You can use this one liner:
$ typeset -i10 x=16#fffff000; echo $x
(This returns an unsigned int for 11.23 and 11.31 ksh. sh returns -4096.)

What was wrong with your adb and $(( )) solutions?
James R. Ferguson
Acclaimed Contributor

Re: convert hexa to decimal values

Hi:

There's nothing wrong with your methodology, only that your numbers exceed 32-bit integers.

Regards!

...JRF...
mobidyc
Trusted Contributor

Re: convert hexa to decimal values

thanks for your answers,

i know now that this is because my numbers exceed 32-bit integers.

do you have a solution for convert these values correctly ?
my system is 64bits, is there a 64bit binary who can calculate the result?

the result of 0xfffff000 would be 4294963200, not -4096

and why
Best regards, Cedrick Gaillard
Bill Hassell
Honored Contributor

Re: convert hexa to decimal values

bc is your friend. Although ksh93 does exist on HP-UX (skillfully hidden away in /usr/dt/bin/dtksh) it is also limited to 32 bit numbers. bc can handle large numbers:

$ echo "4294963200*1234.234"|bc
5300989610188.800


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor
Solution

Re: convert hexa to decimal values

Hi (again) Cedrick:

In the shell, using 'bc' is an easy way:

# echo "obase=10;ibase=16;FFFFF000"|bc

...note the uppercase hexadecimal digits.

Regards!

...JRF...
mobidyc
Trusted Contributor

Re: convert hexa to decimal values

Hello,

as indicated behind, i've tried bc, maybe with a wrong syntax:

# echo "obase=86 ; fffff000/1024/1024" |bc
syntax error on line 1,

i've just looking something, at home i've a linux, it have a 32bit processor however, it can convert without problem so, where is the limitation ?

$> uname -s ; printf "%d\n" 0xfffff000
Linux
4294963200

# uname -s ; printf "%d\n" 0xfffff000
HP-UX
printf: Error converting 0xfffff000
2147483647

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
James R. Ferguson
Acclaimed Contributor

Re: convert hexa to decimal values

Hi (again) Cedrick:

Use UPPERCASE hexadecimal digits with 'bc' as I showed above.

Regards!

...JRF...
mobidyc
Trusted Contributor

Re: convert hexa to decimal values

Big thanks James,

the uppercase letter give me no errors from bc now and correct my [io]base variables has gived to me a good result.

problem is solved, thanks ;)

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
Dennis Handly
Acclaimed Contributor

Re: convert hexa to decimal values

>at home i've a linux, it have a 32bit processor however, it can convert without problem so, where is the limitation?

Does it handle arbitrary 64 bit numbers? Like the 11.23 ksh?

I guess it depends on the standard and whether it is worth it to extend it to long longs.