Forum Moderators: coopster

Message Too Old, No Replies

convert raw values to decimal?

         

kristo5747

9:47 pm on Jul 7, 2010 (gmt 0)

10+ Year Member



Greetings,

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.

rainborick

12:16 am on Jul 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm a linear thinker with modest programming skills, so I'd start by brute-forcing the problem and then see if a more elegant solution presents itself along the way.

Gobble the 4-character words.
Split them in two.
Calculate the separate values and then combine them to get the final result.
Loop until done.


while ($data = read()) {
$word = substr($data, 0, 4); // Gobble the 4-character words.
$lowOrder = hexdec(substr($word,0, 2)); // Split them in two.
$highOrder = hexdec(substr($word, 2, 2)) * 256; // Calculate the separate values
$value = $highOrder + $lowOrder; // Combine them to get the final result
saveResult();
} // end while

kristo5747

2:51 am on Jul 8, 2010 (gmt 0)

10+ Year Member



Using this approach worked

<?php
$line = 'A92500000000';
$hex = implode(array_reverse(str_split($line,2)));
echo hexdec($hex);
?>


Thanks for your input.