How can I convert a decimal number to hex and assign the result to a scalar?
Printable View
How can I convert a decimal number to hex and assign the result to a scalar?
perl -e "printf(\"%x\", 31);"
will give you your conversion from decimal to hex.
So try:
$scalar = sprintf("%x", $decimal);
cudabean
Thanks. It works good.
One more thing.
How do I expressing negative decimal numbers in hex format?
Perl does it for you. Try it.
cudabean
yeah I know that.
I am working on some coding for my shareware app that uses the hard drive serial number. I dont know a lot about hex or perl. I must write a perl script so when people buy my software they get their unlock code quick.
My code messes up on negative numbers and i am just wondering how to even show a neg in hex.
So, when you do this:
perl -e "printf(\"%x\", -31);"
You get:
ffffffe1
Right?
ffffffe1 is a really really big number, except when you're talking signed integers. "Signed integers" means the way that most computers internally represent integers.
When you have signed integers, the left-most bit (MSB) is the sign bit, but it's not as simple as turning the sign bit on in order to convert the number to negative. To negate a number you take the two's compliment of it.
There are no doubt better tutorials on the web than my ramblings here.
cudabean
yeah I was getting lots of fffff. It was really annoying because i need to keep the number down to 8 digits. Thanks for all your help.