What's new

Signed Decimal to 32 Bit Hex

  • Thread starter JustChilling
  • Start date
  • Views 510
J

JustChilling

Enthusiast
Messages
89
Reaction score
45
Points
70
Sin$
0
Okay, so I have a program that needs to be able to take large decimal hashes such as -536774902 and turn them into the signed hexadecimal representation E001770A


Now I didn't just come straight here with my problem, I have had this bug for over a month now and never could fix the issue. I would really appreciate any help you guys can give. I am writing the script in PHP, but the PHP and C/C++/C# libraries are very similar so help in any language would be greatly appreciated.

I would put up a function for what I had so far, but I tried dozens of ways and none of them worked so I got rid of them all. It's able to run on a 32 bit machine (I know because the Xbox 360 runs it) so there must be a way to convert it. I already tried several methods involving two's compliment and they all came out with the unsigned hexadecimal version.

I honestly think this is a unique problem because one of the first hits on Google was a broken function I put on pastebin to show to a friend who tried to help. So I doubt anyone has done this before on here, but if you know how or think you know how please help me out here. Thanks​
 
ObscureCoder

ObscureCoder

Enthusiast
Messages
684
Reaction score
308
Points
125
Sin$
0
You're using PHP, right?
I did a project that used this a while ago and it's the simple function dechex():
PHP:
echo dechex(-536774902);
Output:
Code:
e001770a
 
J

JustChilling

Enthusiast
Messages
89
Reaction score
45
Points
70
Sin$
0
You're using PHP, right?
I did a project that used this a while ago and it's the simple function dechex():
PHP:
echo dechex(-536774902);
Output:
Code:
e001770a
PHP's website said it treated negative numbers as unsigned, IE removing the - sign and continuing. I could have sworn this didn't work on negative hashes.... My bad. Thanks for the help man!

Do you know the mechanics behind this? Like will it work for all negative decimals?
 
yannickky

yannickky

Enthusiast
Messages
304
Reaction score
37
Points
85
Sin$
0
PHP's website said it treated negative numbers as unsigned, IE removing the - sign and continuing. I could have sworn this didn't work on negative hashes.... My bad. Thanks for the help man!

Do you know the mechanics behind this? Like will it work for all negative decimals?

It should work for the negative decimals which fit in the size it can define. don't know by heart how big that (small in this case I guess), since it's been some time since I last used PHP, but it should work under normal circumstances!
 
Y

YYes

Enthusiast
Messages
177
Reaction score
52
Points
85
Sin$
7
PHP's website said it treated negative numbers as unsigned, IE removing the - sign and continuing. I could have sworn this didn't work on negative hashes.... My bad. Thanks for the help man!

Do you know the mechanics behind this? Like will it work for all negative decimals?
Making a signed figure unsigned does not equal removing the negative sign; that's abs().
 
J

JustChilling

Enthusiast
Messages
89
Reaction score
45
Points
70
Sin$
0
Making a signed figure unsigned does not equal removing the negative sign; that's abs().
Well the difference between sign and unsigned when converting decimal to hex would depend on the negative sign. As far as I can tell, if it's negative then it becomes 0xFFFFFFFF - |N|. PHP's website said that dechex() considered all numbers to be unsigned, that is it would treat -11 as just 11, so converted to hex would be 0xB instead of 0xFFFFFFFF - 0xB. This was just for a project editing GTA V scripts and the script VM used an opcode that pushed a 32 bit signed number onto the stack, I tried several alternatives to dechex but some negative hashes were still having issues. I went back to trying dechex and I guess it does create a 32 bit signed number. Thank you all for the help!
 
Y

YYes

Enthusiast
Messages
177
Reaction score
52
Points
85
Sin$
7
Well the difference between sign and unsigned when converting decimal to hex would depend on the negative sign. As far as I can tell, if it's negative then it becomes 0xFFFFFFFF - |N|. PHP's website said that dechex() considered all numbers to be unsigned, that is it would treat -11 as just 11, so converted to hex would be 0xB instead of 0xFFFFFFFF - 0xB. This was just for a project editing GTA V scripts and the script VM used an opcode that pushed a 32 bit signed number onto the stack, I tried several alternatives to dechex but some negative hashes were still having issues. I went back to trying dechex and I guess it does create a 32 bit signed number. Thank you all for the help!
Ah alright, cool cool. I've only ever heard signed or unsigned in terms of bits, that's what I was thinking. Chill, glad you got it working.
 
Top Bottom
Login
Register