Bit shifting an INT
Posted: Mon Sep 16, 2013 3:37 pm
I have a socket that I need to send data to at byte level. I'm looking to send 6 bytes over the socket and set each byte to a specific value using bit shifting and/or the pack method (I think).
I understand that a php int is 32 bytes. Logically, I'm looking to do something like this but can't seem to get the right value.
I found something that used this notation which I also tried but don't quite understand.
any ideas how I can achieve this?
I understand that a php int is 32 bytes. Logically, I'm looking to do something like this but can't seem to get the right value.
Code: Select all
$size= 50;
$type = 1
$buffer[0] = $size >> 24;
$buffer[1] = $size >> 16;
$buffer[2] = $size >> 8;
$buffer[3] = $size >> 0;
$buffer[4] = $type >> 24;
$buffer[5] = $type >> 0;
Code: Select all
$data= pack("CCCn", 0xFF, $type, ($type >>24), ($size >>24), ($size >>16), ($size >>8), ($size >>0)); // positive this is not correct