php包:数据类型和结果验证方面的问题

php包:数据类型和结果验证方面的问题,php,pack,Php,Pack,我是PHP初学者,我的任务是构建命令,这些命令稍后将通过UDP发送到设备。运行:OSX、PHP5.5.3.8 要创建二进制数据,我使用“pack”。下面是我的代码示例: <?php $in_1 = 0x3100; $in_2 = 0031; echo "Inputs: " . $in_1 . "\t" . $in_2; $bin = pack("v*", $in_1, $in_2); echo "\nlength: ". strlen($bin); printf ("\npacked re

我是PHP初学者,我的任务是构建命令,这些命令稍后将通过UDP发送到设备。运行:OSX、PHP5.5.3.8 要创建二进制数据,我使用“pack”。下面是我的代码示例:

<?php
$in_1 = 0x3100;
$in_2 = 0031;
echo "Inputs: " . $in_1 . "\t" . $in_2;
$bin = pack("v*", $in_1, $in_2);
echo "\nlength: ". strlen($bin);
printf ("\npacked result: %x \n",$bin);
printf("HEX result %h\n", $bin);
file_put_contents("ausgabe.log", $bin, FILE_APPEND);
?>
我想知道25美元的号码是多少。 如果我将0x0031赋值给_2中的$2,结果是49。 这里怎么了

顺便说一句:我的最终目标是构建二进制命令,在这样的方案中正好是12字节(每行中的十进制值作为注释):

本例中的结果(十六进制)应如下所示:25 00 00 04 03 06 FF 79 81 23 00


谢谢

对不起,我的回答格式不正确。 以下是请求的代码:

// toggle higher and lower byte of a two-byte short variable
function toggle_two_bytes ($two_bytes)`

{
    $two_bytes = $two_bytes & 0xffff; // limit size to 2 Bytes
    $high = ($two_bytes << 8);          // bit shift
    $low = ($two_bytes >> 8);
    $ret = ($high | $low);              // OR
    $ret = $ret & 0xffff;               // limit (again) to two bytes
    return ($ret);
}
//切换两字节短变量的高字节和低字节
函数切换两个字节($两个字节)`
{
$two_bytes=$two_bytes&0xffff;//将大小限制为2个字节
$high=($2_字节>8);
$ret=($high |$low);//或
$ret=$ret&0xffff;//将(再次)限制为两个字节
回报($ret);
}

我发现使用pack('v',…)这不再是必要的。

我想出了这个,但它不是最好的方法,正确的按位操作会更快

我正在使用
sprintf
%x
格式说明符,该说明符将值转换为十六进制值

每个转换规范包含一个百分号(%)

您将看到我使用了
%02x
%04x

  • 0
    -左侧用零填充数字
  • 2
    4
    宽度,要打印的最小字符数
  • x
    -十六进制的说明符,大写字母使用
    x
代码:


演示:

好的,第一件事解决了。PHP将带前导零的数字解释为八进制数。因此$a=0123;相当于这里的83位小数示例:当然,您可以发布更多关于
切换两个字节的信息吗。虽然我在pack()中使用了正确的修饰符“v”,但这不再是必需的…//切换两字节短变量函数的高位和低位字节切换两字节($two_字节){$two_字节=$two_字节&0xffff;//将大小限制为2字节$high=($two_字节>8);$ret=($high |$low);//或$ret=$ret&0xffff;//再次限制为两字节返回($ret);}谢谢你的灵感。我终于让
pack()
起作用了。魔法就在这里:
returnpack(“vvCCCCvv”)、$command、$status、$type、$ch、$out_ch、$preset、$value、$seq);
要查看我的结果,我使用
bin2hex()
在bin字符串上。干杯!Stefan.BTW我用wireshark检查了通过UDP发送的字符串-一切都很完美。
function set_routing_item ($in_ch, $out_ch, $on_off, $seq)
{
    $command = toggle_two_bytes(37);    // 37 --> 0x2500
    $status = 0x0000;                   // 0 --> 0x0000
    $pos = 4;                           // 4= route item
    $channel_1 = $in_ch;                // 3
    $channel_2 = $out_ch;               // 6
    $preset_no = 0xff;                  // 255
    $value = $on_off;                   // 33145
    $seq = $seq;                        // 35
    // implement building of command using pack here
}
// toggle higher and lower byte of a two-byte short variable
function toggle_two_bytes ($two_bytes)`

{
    $two_bytes = $two_bytes & 0xffff; // limit size to 2 Bytes
    $high = ($two_bytes << 8);          // bit shift
    $low = ($two_bytes >> 8);
    $ret = ($high | $low);              // OR
    $ret = $ret & 0xffff;               // limit (again) to two bytes
    return ($ret);
}
<?php
function toggle_two_bytes ($two_bytes)
{
    $two_bytes = $two_bytes & 0xffff;   // limit size to 2 Bytes
    $high = ($two_bytes << 8);          // bit shift
    $low = ($two_bytes >> 8);
    $ret = ($high | $low);              // OR
    $ret = $ret & 0xffff;               // limit (again) to two bytes
    return ($ret);
}


function set_routing_item ($in_ch, $out_ch, $on_off, $seq)
{
    $str  = '';
    $command = toggle_two_bytes(37);
    $str .= sprintf('%04X', $command);

    $status = 0x0000;
    $str .= sprintf('%04X', $status);

    $pos = 4;
    $str .= sprintf('%02X', $pos);

    $str .= sprintf('%02X', $in_ch);

    $str .= sprintf('%02X', $out_ch);

    $preset_no = 0xFF;
    $str .= sprintf('%02X', $preset_no);

    $value = toggle_two_bytes($on_off);
    $str .= sprintf('%04X', $value);

    $seq = toggle_two_bytes($seq);
    $str .= sprintf('%04X', $seq);

    for ($i = 0; $i < strlen($str) - 1; $i += 2) {
      echo $str[$i] . $str[$i+1] . ' ';
    }
}

echo set_routing_item(3, 6, 33145, 35) . PHP_EOL;
25 00 00 00 04 03 06 FF 79 81 23 00