Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting signed hex back to a decimal???

Posted on 2002-03-06
4
Medium Priority
?
1,478 Views
Last Modified: 2008-03-06
Hi all, just wondering how to convert a hex number which is negative to it's decimal equivalent.

So if I had the number '0xFE' it would output it's decimal number (which would have originally been negative).

Any ideas?
0
Comment
Question by:OnlyOne
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
japhyRPI earned 200 total points
ID: 6845626
You want to use the hex() function; however, hex("0xFE") would return 254, not -2 (as I assume you would like).  On the other hand, hex("0xFFFFFFFE") still returns 4294967294, not -2.

If you need to enforce signed-ness, that's a different story.  Assuming you have an eight-character hex number (like "FFFFFFFE"), you can use the following code:

  $number =
    unpack "i",      # decode as a signed integer
    pack "I",        # encode as an unsigned integer
    hex("0x$hnum");  # convert 0x........ to integer

When I set $hnum to "FFFFFFFE", I get -2 in $number.
0
 
LVL 1

Expert Comment

by:japhyRPI
ID: 6845632
I just realized that if you have a two-character hex value, such as "FE", you can use the "c" and "C" pack/unpack template.

  $number =       # so it gets -2
    unpack "c",   # unpacked as signed char
    pack "C",     # packed as unsigned char
    hex("0xFE");  # returns 254
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6845635
OnlyOne,

"..convert a hex number which is negative to it's decimal equivalent...."

Here is a small script that might work for you.

#!/usr/local/bin/perl

$hexadecimal='0xFE';
$number = hex($hexadecimal);         # hexadecimal

print $number,"\n";

Hope that helps.

"..(which would have originally been
                     negative)..."

i am not sure if the above code solves this part.
0
 

Author Comment

by:OnlyOne
ID: 6849991
Well done, japhyRPI, that was right on the money.

Keep up the good work!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question