简体   繁体   中英

Convert python pack to php pack

I have this python script

b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii',
                21, 0,
                len(country), country,
                len(device), device,
                len('1.3.1'), "1.3.1",
                len('Web'), "Web",
                27, 0, 0,
                3, 0, cid, lac,
                0, 0, 0, 0)

and I want to convert it to php, this is what I came with so far

$body= pack('nln2c*n13c*n5c*n3c*Ciiiniiiiii',
                    21, 0,
                    strlen($this->_mccToCountry[$this->_mcc]), $this->_mccToCountry[$this->_mcc],
                    strlen($this->_device), $this->_device,
                    strlen('1.3.1'), "1.3.1",
                    strlen('Web'), "Web",
                    27, 0, 0,
                    3, 0, $this->_cellId, $this->_lac,
                    0, 0, 0, 0);

The variables are same as those in python script, but i got this error

PHP Warning: pack(): Type n: too few arguments in .../application/extensions/Zend-extensions/NMS/Bts.php:150

The help will be very appreciated.

Your parameter string is all screwed up. You indicate in places that you are going to pass 2, 5, 3 and 13 shorts, but only provide one each time. You indicate that you are going to provide a series of characters but then you provide a NUL terminated string. You indicate that you will be providing an unsigned char but don't provide one. Try this format string instead:

'nlna*na*na*na*i3ni6'

Another option is to use serialize and deserialize instead. Not sure if it matters to you that the data is packed into bytes or serialized into strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM