DasKaktus Posted March 17, 2016 Share Posted March 17, 2016 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,0x01, pack("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. Quote Link to comment https://forums.phpfreaks.com/topic/301028-converting-pythin-to-php/ Share on other sites More sharing options...
requinix Posted March 17, 2016 Share Posted March 17, 2016 .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. Quote Link to comment https://forums.phpfreaks.com/topic/301028-converting-pythin-to-php/#findComment-1532118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.