Jump to content

Creating Packets


DKP
 Share

Recommended Posts

I've got an existing C application that I'm trying to send a message to from PHP. Unfortunately the packet layout is a series of integers, and I also need to byte swap the data. If I type out the correct character values in a string like the following it works, but I'm hoping for a less code intensive solution.$packet = "";$packet .= chr(0);$packet .= chr(101);...I wrote the following function that creates the packet from an array of integers.

function create_packet($iarray) {    $packet = "";    foreach ($iarray as $ival) {        $packet .= chr(((int)$ival & 0xff000000) >> 24);        $packet .= chr(((int)$ival & 0x00ff0000) >> 16);        $packet .= chr(((int)$ival & 0x0000ff00) >> 8);        $packet .= chr((int)$ival & 0x000000ff);    }    return $packet;}

When I try it w/ the values of one packet (101, 0, 43, 4200, 0, 0) it works fine, but when I try it with another (101, 0, 43, 4300, 0, 0) it fails. I wrote a similar function in C++ and it does give me the correct numbers. I believe the difference is that in the C++ function I was able to type cast the values to signed characters (i.e. char). So in the C++ the number 4300 gets broken up into 0 0 16 and -52, while w/ the PHP it ends up being 0 0 16 and 204.I don't see that char is a data type available to me in PHP. Is there another way to make this function work? Or is there an easier way to achieve the same result?

Link to comment
Share on other sites

So in the C++ the number 4300 gets broken up into 0 0 16 and -52, while w/ the PHP it ends up being 0 0 16 and 204.
I'm not sure I see the relationship there. If 4300 maps to 0 0 16 -52, those are the results of the 4 masks and bit shifts, right? I may need to write this out.So 4300 is this:0001 0000 1100 1100So the first 2 masks and shifts will each result in 0, the third one will be 0001 0000 or 16, and the last mask, without the bit shift, is going to give 1100 1100, which is 204, at least unsigned it is. But what you're asking is that you want 1100 1100 to represent a signed int instead of unsigned int, correct? PHP doesn't have a char data type, but the char data type in C is still just represented as an int, it's just an int that maps to a character table.
Link to comment
Share on other sites

Yes, the bit pattern for the integer 4200 is as follows, which results in the following signed and unsigned character values.

4200 -> 00000000 00000000 00010000 01101000char           0        0       16      104      uchar          0        0       16      104

However, the integer value 4300 results in

4300 -> 00000000 00000000 00010000 11001100char           0        0       16      -52uchar          0        0       16      204

So, while I believe the bit pattern (as a result of the masks and shifts) of the last byte of 4300 is correct, its value obviously changes depending on how the bit pattern is interpreted as shown above. Since the chr() function takes an integer I'm assuming the bit pattern for the last byte would be interpreted as 204, not -52.So, since I can't cast something to an char value, I guess my question is is there another way to put this bit pattern in a string (i.e. packet) so that I can use the php socket commands to send this message?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...