Jump to content

Converting Pythin to PHP


DasKaktus

Recommended Posts

Hi.

 

First of all, i'm not sure if this is the right place to ask this question, but "Other Languages" seemed wrong also.. But here goes.

 

I'm trying to convert a long Python script to PHP.

 

Its using sockets to send packages. But I'm stuck.

 

This is the Python code im stuck with:

 

to_checksum = bytearray()
to_checksum.append(0x68)
to_checksum.append(0x32)
to_checksum.append(0x01)
to_checksum.append(0x7b)
to_checksum.append(0x01)
to_checksum.extend(struct.pack("<h", gif_len))
to_checksum.append(packet_num)
to_checksum.append(last_packet_num)

 

I figured I would have to use PHP's pack also for this.

But I have never used (or had a need to use) pack and unpack.

 

I've sort of tried:

 

$bindata = pack("s",0x68,0x32,0x01,0x7b,0x01) ;

 

But then i Would ahve to do this which dont seem right;

 

$bindata pack("h",0x68,0x32,0x01,0x7b,0x01pack("h", 10))

 

for the first part. but then python started to merge extend with append, which I have no clue what it does.

 

So if there are anybody that is great with Python and PHP that could try to explain to me how to do it or how to find information on how to do it, that would be awsome.

Link to comment
Share on other sites

.extend() is basically a .append() that works with arrays.

 

Assuming you're using $to_checksum as a string instead of an array,

$to_checksum = "\x68\x32\x01\x7b\x01";

// to_checksum.extend(struct.pack("<h", gif_len))
//  < = little-endian, h = signed short (2 bytes)
// php's pack() doesn't have a little-endian signed short code, so convert signed to unsigned and use 'v'
$to_checksum .= pack("v", ($gif_len < 0 ? 65536 + $gif_len ? $gif_len));

$to_checksum .= chr(packet_num) . chr(last_packet_num);
I'm a bit unsure of that last line.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.