I'm trying to extract packed hexadecimal numbers from a string. My application is communicating with a server which sends a string with a header followed by 2 byte packed hexadecimal numbers. There are thousands of numbers in this string.
What I want to do is extract each 2 byte compressed number, and convert that into a number I can use to perform calculations on.
Example: string = "info:\x00\x00\x11\x11\x22\x22"
will produce three numbers 0x0000
(decimal 0), 0x1111
(decimal 4369), 0x2222
(decimal 8738)
I have a working solution (see below,) but it functions too slowly when I try to process the several thousand numbers that the server sends over. Please provide some recommendations to speed up my approach.
//Works but is too slow!
//$string has the data from the server
$arrayIndex = 0;
for($index = [start of data]; $index < strlen($string); $index+=2){
$value = getNum($string, $index, $index+1);
$array[$arrayIndex++] = $value;
}
function getNum($string, $start, $end){
//get the substring we're interested in transforming
$builder = substr($string, $start, $end-$start+1);
//convert into hex string
$array = unpack("H*data", $builder);
$answer = $array["data"];
//return the value as a number
return hexdec($answer);
}
I've also been attempting to extract the numbers in a single unpack command, but that is not working (I'm having some trouble understanding the format string to use)
//Not working alternate method
//discard the header (in this case 18 bytes) and put the rest of the
//number values I'm interested in into an array
$unpacked = unpack("c18char/H2*data", $value);
for($i = 0; $i < $size; $i+=1){
$data = $unpacked["data".$i];
$array[$i] = $data;
}