kristo5747 Posted July 7, 2010 Share Posted July 7, 2010 Gurus, I receive daily files that contain raw values in little endian format. My job is load the files into a database table (that's the easy part). The values in the file look like this: A92500000000 (translates to 0x25A9 in hex which translates to 9,641 in decimal) 851E00000000 (translates to 0x1E85 in hex which translates to 7,813 in decimal) ... I started reading about unpack but the different format options are making my head spin. What would be the best way to convert these values to decimal? Any ideas? Thanks. Al. Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/ Share on other sites More sharing options...
ChemicalBliss Posted July 7, 2010 Share Posted July 7, 2010 If your converting from Hex to Dec: http://uk.php.net/manual/en/function.hexdec.php If your trying to get the hex from those lines: $line = chunk_split($line, 2); // $line would be a single string line: A92500000000 $hex = implode(array_reverse($line)); // $hex would result as: 0000000025A9 (which should be able to pass through hexdec(); echo(hexdec($hex)); -cb- Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1082768 Share on other sites More sharing options...
kristo5747 Posted July 7, 2010 Author Share Posted July 7, 2010 Thanks for your reply. Unfortunately, there is a problem <?php $line = "A92500000000"; $line = chunk_split($line, 2); // $line would be a single string line: A92500000000 /* *I added to this since implode() requires an array as argument */ $line = array(); $hex = implode(array_reverse($line)); // $hex would result as: 0000000025A9 (which should be able to pass through hexdec(); /* *echoes BLANK *echo(hexdec($hex)); */ /* *I modified your suggestion to this */ var_dump(implode(array_reverse($line))); ?> The output is string(0) "" Any idea what could be the problem?? Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1082775 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 The problem is that the chunk_split function doesn't return an array, you want to use str_split instead. <?php $line = 'A92500000000'; $hex = implode(array_reverse(str_split($line,2))); echo hexdec($hex); ?> Ken Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1082800 Share on other sites More sharing options...
kristo5747 Posted July 8, 2010 Author Share Posted July 8, 2010 Wow!! Thank you. It worked. Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1082829 Share on other sites More sharing options...
ChemicalBliss Posted July 8, 2010 Share Posted July 8, 2010 FYI - The line you added "$line = array();" broke the script. Also, Kens example is a much shortened less commented version of my script with the _only_ difference being the function used to split the string, which in this case makes no difference. -cb- Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1083132 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 The use of chunk_split vs str_split does make a difference. The function chunk_split() does not return an array so the array_reverse function gets an error. The function str_split() does return an array. Ken Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1083140 Share on other sites More sharing options...
ChemicalBliss Posted July 8, 2010 Share Posted July 8, 2010 Ah that's strange i swore chunk split returned an array, ah well learn something new yada yada. -cb- Link to comment https://forums.phpfreaks.com/topic/207071-newbie-question-convert-raw-values-to-decimal/#findComment-1083155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.