Operating System - Linux
1828390 Members
2926 Online
109977 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.

Michael D. Zorn
Regular Advisor

Re: convert hexa to decimal values

HP 11.11 here.
#bc
ib=16
FFFFF000
4294963200
^D
#

If you're converting not to many numbers, you could try

#perl -e '$x=0xFFFFF000;print "$x\n"'
429496320
#

If it's a very few numbers, you can go to basics:

FFFFF000 = FFFFFFFF - FFF

FFFFFFFF is one less than x100000000, which is 16^8 (=4294967296), or 4294967295

FFF is one less than x1000 (4095)

4294967295 - 4095 = 4294963200

Or even:
16^8 - 16^3 = 4294963200

(In hex, that's
100000000 - 1000 = 0xFFFFF000)

Here's a short perl routine:

$!/usr/bin/perl
while () {
print hex "$_"; print "\n";
}

(For some odd reason, this doesn't work:
print hex "$_\n";
)

and you can always change to read from a file.
James R. Ferguson
Acclaimed Contributor

Re: convert hexa to decimal values

Hi:

If you wish to use Perl's 'hex' function you can do:

# echo FF|perl -nle 'print hex'
255

(or):

# perl -le 'print hex $ARGV[0]' FFFF
65535

Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: convert hexa to decimal values

As for the Perl variants,
if you give it as a literal there's even no need for a call of oct() or hex().

$ perl -le 'print 0xfffff000'
4294963200

Obviously, this doesn't work anymore if read from stdin or a file.

$ perl -le 'print shift' 0xfffff000
0xfffff000

then you have to use oct or hex

$ perl -le 'print hex shift' 0xfffff000
4294963200

$ perl -le 'print oct shift' 0xfffff000
4294963200


Madness, thy name is system administration