繁体   English   中英

php pack:数据类型问题和我的结果验证

[英]php pack: problems with data types and verification of my results

我是PHP初学者,我的任务是构建命令,稍后将通过UDP发送到设备。 运行:OSX,PHP 5.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);
?>

我终端的输出是:

Inputs: 12544   25
length: 4
packed result: 0 
HEX result 

我想知道$ in_2的数字是25。 如果我将0x0031分配给$ in_2,结果为49.这里有什么问题?

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

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
}

在这种情况下(十六进制)的结果应如下所示:25 00 00 00 04 03 06 FF 79 81 23 00

谢谢!

我想出了这个,但这不是最好的方法,适当的按位操作会更快。

我正在使用带有%x格式说明符的sprintf ,它将值转换为十六进制值。

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

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

  • 0 - 左键用零填充数字
  • 24 - 宽度,即要打印的最小字符数。
  • x - 十六进制的说明符,使用X表示大写。

码:

<?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

演示: https//eval.in/793695

抱歉我的格式错误答案。 这是请求的代码:

// 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);
}

我发现有了pack('v',...),这已经不再需要了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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