PHP包功能不像字符串

时间:2014-12-12 20:45:49

标签: php pack

需要PHP中的pack()函数的一些帮助。文件超出了我的想法..

我有以下代码:

$arr_upc = array();
    if ($user_pass_code !== '' )
    {
        $arr_upc = str_split($user_pass_code); // Split provided pass code into char array for building the command
    }else{
        $arr_upc = str_split('0000'); // Provide an invalid code by default
    }

    //$arr_upc[0], $arr_upc[1], $arr_upc[2], $arr_upc[3]
    $num1 = '0x0' . $arr_upc[0];
    $num2 = '0x0' . $arr_upc[1];
    $num3 = '0x0' . $arr_upc[2];
    $num4 = '0x0' . $arr_upc[3];

    $cmd = pack("C*", 0x78, 0x08, 0x10, 0x00, 0x08, 0x01, 0x05, 0x00, 0x0E, 0x53);

    //$cmd = pack("C*", 0x78, 0x08, 0x10, 0x00, $num1, $num2, $num3, $num4, 0x0E, 0x53); // WHAT I'D LIKE TO USE BUT NOT WORKING

当我像这样使用它时: $ cmd = pack(" C *",0x78,0x08,0x10,0x00,0x08,0x01,0x05,0x00,0x0E,0x53);

工作正常。

当我尝试用字符串变量替换该列表中的某些项时,它会失败: $ cmd = pack(" C *",0x78,0x08,0x10,0x00,$ num1,$ num2,$ num3,$ num4,0x0E,0x53); //失败

我猜测包不喜欢它或我需要在将其输入函数之前将其转换为某些内容,但我现在已经丢失了。

1 个答案:

答案 0 :(得分:1)

尝试此更改:

$num1 =  0+'0x'.$arr_upc[0];
$num2 =  0+'0x'.$arr_upc[1];
$num3 =  0+'0x'.$arr_upc[2];
$num4 =  0+'0x'.$arr_upc[3];