我正在尝试一段代码。
<?php
$tmp = ord('F'); //gives the decimal value of character F (equals 70)
$tmp = $tmp - 55; //gives 15 - decimal equivalent of 0x0F
$tmp = dechex($tmp); // converts 15 to 0x0F
$fp = fopen("testing.data","wb+");
fwrite($fp,$tmp);
fclose($fp);
?>
当我在十六进制编辑器中打开名为 testing.data 的文件时,我看到写入了 2 个字节。 2 个字节是 0x36 和 0x33。 我期望只有 1 个字节,即 0x0f 将被写入文件。这不会发生。 请帮我解决这个问题。
最佳答案
如果要将字节 0x0f
写入文件,只需用该 ASCII 码写入字符即可。您实际上想要撤消 ord
,反向函数是 chr
:
<?php
$tmp = ord('F'); //gives the decimal value of character F (equals 70)
$tmp = $tmp - 55; //gives 15 - decimal equivalent of 0x0F
$tmp = chr($tmp); // converts 15 to a character
$fp = fopen("testing.data","wb+");
fwrite($fp,$tmp);
fclose($fp);
?>
关于php - 将十六进制数据写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973830/