Link to home
Start Free TrialLog in
Avatar of sanek
sanek

asked on

Big numbers in perl.

How can I convert big numbers from decimal to Hex?
Avatar of ozo
ozo
Flag of United States of America image

Do you mean Math::BigInt numbers?
Avatar of sanek
sanek

ASKER

I've pretty long string of \d chars that cannot be converted with sprintf("%X", $str).
Let's say that decimal value can reach upto 2^64 (8 bytes)
perl -MMath::BigInt=:constant -e '$x=shift; printf"%08X%08X\n",$x/2**32,$x%2**32' 9876543210987654321
Avatar of sanek

ASKER

Hmm... That's doesn't work :(
It ouputs result 891087B87FFFFFFF
And it's always 7fffffff in $x%2**32
Sounds like you left out the -MMath::BigInt=:constant
Avatar of sanek

ASKER

I've just copy&past-ed the whole string into command line. I understand this is the way that should work.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sanek

ASKER

I get
+2299561912+3820424369
In a case I substitute "%s%s" with "%X%X" - I receive:
891087B8E3B70CB1

What, I suppose, is correct.
Thanks, ozo.
You probably want to substitiute %08X%08X, in case you get a number like 9876543207167229953
I guess you must have a version of Math::BigInt that doesn't support :constant, sorry.
Avatar of sanek

ASKER

Oh yeah, thanks.