469,326 Members | 1,530 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Converting hex string to 32 bit signed integer

5
I have a hex value FFFFFFCE that actually represents -50. When I simply use hex function to convert the above value to an integer, I get 4294967246. That means that integer overflow took place. How do I handle such cases, so that conversion will be correct?
Aug 16 '07 #1
9 32019
KevinADC
4,059 Expert 2GB
I thought 50 in hex is 32? (16x3+2)
Aug 16 '07 #2
whirly
5
thanks for your reply. Actually, it is negative 50. I know that if I do the following: $t = hex('ffffffce') & hex('7ffffffff'); $t = $t - hex('7fffffff') - 1;, I get the correct value of negative 50. But this is clunky.
Aug 16 '07 #3
KevinADC
4,059 Expert 2GB
I'm lost......... :(
Aug 16 '07 #4
whirly
5
Sorry. Let me try to clarify. I am trying to convert 8-character hex values to their 32-bit integer representation. The problem arises when a hex string starts with FFFF (ex. FFFFFFCE). I know that the real answer once the conversion is done is "-50" for "FFFFFFCE". You can check it by entering "-50" into any calculator that does conversions, and you will see that the hex value for it is "FFFFFFCE". But if you enter "FFFFFFCE" first and try to convert it to an integer, you will get "4294967246", which indicates overflow. My current solution is to subtract the maxint value (FFFFFFFF) from the given value (FFFFFFCE) and to subtract 1 from the result, which will yield the correct value of "-50". I don' t know if that is the best way and also if there are any other gotchas. I would imagine underflow will also present problems.
Aug 16 '07 #5
numberwhun
3,503 Expert Mod 2GB
Have you tried using the Data::Translate module from cpan? It looks like it does conversions like the one you are looking to do.

Regards,

Jeff
Aug 17 '07 #6
whirly
5
Thank you, Jeff. I will check it out.
Aug 17 '07 #7
miller
1,089 Expert 1GB
Sorry. Let me try to clarify. I am trying to convert 8-character hex values to their 32-bit integer representation.
Actually, what you are trying to do is convert an 8 character hex string to a 32-bit SIGNED integer, at least according to the specification from Java or C. The problem you have is that there is no such thing as a strictly 32-bit integer in perl, so you must code the rules yourself or use a module as Jeff suggested.

Fortunately, the rules are fairly simple. But let's start with unsigned first:
unsigned int
Dec: 0 to 4_294_967_295 (2^32-1)
Hex Representation: 0x00000000 to 0xFFFFFFFF
As you can see, straight forward enough. And the hex translation can be handled directly with the hex function and sprintf.

Signed integers are a little harder. The positive range is handled easily enough. However, the negative range is indicated by the 32nd bit, and the values are stored opposite of the positive range. This made arithmetic simpler back in the "old days" I suspect.

signed int
Dec: (-1 * 2 ^ 31) -2_147_483_648 to 2_147_483_647 (2 ^ 31 - 1)
hex positive: 0x00000000 to 0x7FFFFFFF
hex negative: 0xFFFFFFFF to 0x80000000
The easiest way to code your own translation function would be to rely on a little bit arithmetic:

Expand|Select|Wrap|Line Numbers
  1. print hexstr_to_signed32int('FFFFFFCE'), "\n"; # Outputs -50
  2. print signed32int_to_hexstr(-50), "\n"; # Outputs ffffffce
  3.  
  4. sub hexstr_to_signed32int {
  5.     my ($hexstr) = @_;
  6.     die "Invalid hex string: $hexstr"
  7.         if $hexstr !~ /^[0-9A-Fa-f]{1,8}$/;
  8.  
  9.     my $num = hex($hexstr);
  10.     return $num >> 31 ? $num - 2 ** 32 : $num;
  11. }
  12.  
  13. sub signed32int_to_hexstr {
  14.     my ($num) = @_;
  15.     die "Number outside of signed 32-bit range: $num"
  16.         if $num > 2_147_483_647 or $num < -2_147_483_648;
  17.  
  18.     my $unsigned = $num < 0 ? 2 ** 32 + $num : $num;
  19.     return sprintf "%x", $unsigned;
  20. }
  21.  
If you wanted capital hex strings, just change %x to %X

- Miller
Aug 23 '07 #8
whirly
5
Thank you so much!!!
Sep 6 '07 #9
Really great answer! Your thorough explanation helped me with a loosely related issue in creating a hashing function compatible with ones we use in Java and C. Perl had the binary result intact, but just needed a little adjustment to represent it as an unsigned integer value. Much appreciated.

The problem you have is that there is no such thing as a strictly 32-bit integer in perl, so you must code the rules yourself or use a module as Jeff suggested.
...
Expand|Select|Wrap|Line Numbers
  1.     return $num >> 31 ? $num - 2 ** 32 : $num;
  2.  
- Miller
Jul 25 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by ken | last post: by
5 posts views Thread by nickisme | last post: by
2 posts views Thread by Asbjørn Ulsberg | last post: by
7 posts views Thread by Robin Haswell | last post: by
11 posts views Thread by TomServo | last post: by
5 posts views Thread by Hans Mull | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.