Jump to content

newbie question: convert raw values to decimal?


kristo5747

Recommended Posts

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.

 

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-

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??

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-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.