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

标签 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,$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/

相关文章:

php - 让上一个和下一个按钮正常工作

php - 在mysql和PHP中检查一个字段是否有属性 `UNIQUE`

mysql - 使用 PDO 的 undefined variable

php - Storage::delete 不删除文件。路径正确,文件权限正确

php - 如何使用 PSR-7 响应?

c# - 你能在 C# 中做 $array ["string"]

php - 我的 MySQL 准备好的语句不起作用

php - Swig 将 vector<std::string> 转换为原生 PHP 数组

php - 防止 netbeans php Debug 在浏览器中打开新选项卡

javascript - 如何使用 HTML5 重命名下载的文件?