将十六进制数除以2并在每个之前添加/ x

时间:2014-03-26 21:55:39

标签: php

我正在尝试下面的任务, 得到字符串的长度---->添加一些东西---->改为十六进制---->写出结果如

例如:dechex结果等于01a3cd - >输出应该看起来像/ x01 / xa3 / xcd 但这可以是n个字节

下面是我写的示例代码,但我知道如何做最后一部分

$message="1 1,1,1,0,0,2,0,1000,0,2,0,1,,,,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,,,,,,2,,,0,0,2,1,0,0,0,0,0,0,1,0,3,0,0,0,0,,,0,\"\\x0d\\x0dtest1\",0,800,,0,0,,0,0,0,0,0,0,,,,,0,0,0,0,1,0,,0,0,0,0,0,0,0,0,0,1,,0,,,,0,0,,,,0,0,0,0,0,\x10\x01\x00\x00\x00\x00\x00\x00\x00\xe0 2,2,2,0,0,2,0,1000,0,2,0,1,,,,0,0,0,0,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,0,0,,,,,,2,,,0,0,2,1,0,0,0,0,0,0,1,0,3,0,0,0,0,,,0,\"\\x0d\\x0dplu2\",0,100,,0,0,,0,0,0,0,0,0,,,,,0,0,0,0,1,0,,0,0,0,0,0,0,0,0,0,1,,0,,,,0,0,,,,0,0,0,0,0,";
$b=(strlen($message));

echo dechex($b);
echo dechex($b+18);
str_split (dechex($b));

谢谢

1 个答案:

答案 0 :(得分:0)

要以您需要的格式输出值的两个低字节(假设该值适合2个字节),您可以使用sprintf;

echo sprintf('\\x%02x\\x%02x', $b/256, $b%256);

>> \x01\xcc