加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将十六进制数据写入文件

发布时间:2020-12-13 22:03:31 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试一段代码. ?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,$
我正在尝试一段代码.

<?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",$tmp);
fclose($fp);
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读