Combine bytes php -


i need equivalent of part (in c++), on php:

unsigned char arr[ 2 ]; arr[ 0 ] = 0x04; arr[ 1 ] = 0x00; unsigned short shape_types; memcpy( &shape_types, arr, 2 ) 

maybe knows program, can combine bytes , see result in hex , dec?

the code have given not directly applicable php.

php lacks typing or ability move data directly 1 memory location this.

i guess want pretty following.

your input array of bytes: (include 4 bytes 32 bit systems, 8 64 bit)

$bytes = array(     0x04,     0x00 ); 

you can reduce loop:

$result = 0; foreach ($bytes $byte) {     $result = $result << 8 | $byte; } 

or array_reduce:

$result = array_reduce(     $bytes,     function ($out, $in) {         return $out << 8 | $in;     } ); 

or combine values directly:

$result = $bytes[0] << 8 | $bytes[1]; 

display in hex:

var_dump(dechex($result)); 

output:

string '400' (length=3)


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -