sanek
asked on
Big numbers in perl.
How can I convert big numbers from decimal to Hex?
Do you mean Math::BigInt numbers?
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)
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**3 2,$x%2**32 ' 9876543210987654321
ASKER
Hmm... That's doesn't work :(
It ouputs result 891087B87FFFFFFF
And it's always 7fffffff in $x%2**32
It ouputs result 891087B87FFFFFFF
And it's always 7fffffff in $x%2**32
Sounds like you left out the -MMath::BigInt=:constant
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
+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.
I guess you must have a version of Math::BigInt that doesn't support :constant, sorry.
ASKER
Oh yeah, thanks.