adammw Posted April 6, 2009 Share Posted April 6, 2009 Hi. I've recently been having trouble with my latest project, where I'm trying to get information from the game, OpenTTD. So far so good, except I can't figure out how to handle negative numbers. All numbers are sent as little-endian in hex, so it makes it really confusing. So far I have, function unle($str) { // This function converts a little endian string into a decimal number for ($i = 0; $i < strlen($str); $i++) { $hex[$i] = str_pad(dechex(ord($str[$i])),2,'0',STR_PAD_LEFT); } $hex = array_reverse($hex); $val = hexdec(implode($hex)); return $val; } which works perfectly for normal positive values but when it hits a negative value (such as 1ef6ffffffffffff ) it goes crazy and comes back with a float(1.84467440737E+19) - according to the game I'm pretty sure it should be -2530. I've tried converting to binary and back and using the "complement" but I don't think i was doing that right and it didn't work. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/152738-unsigned-hex-numbers-with-a-negative-value/ Share on other sites More sharing options...
Daniel0 Posted April 6, 2009 Share Posted April 6, 2009 Try this: http://stackoverflow.com/questions/610603/help-me-translate-long-value-expressed-in-hex-back-in-to-a-date-time Quote Link to comment https://forums.phpfreaks.com/topic/152738-unsigned-hex-numbers-with-a-negative-value/#findComment-802094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.