【发布时间】:2015-11-14 17:17:26
【问题描述】:
我正在编写一种十六进制查看器,用户在其中输入可执行文件,页面返回十六进制转储和旁边的 ANSI 表示。 (其实我不知道为什么要使用ANSI,但是我使用的十六进制编辑器使用这个返回结果)
类似这样的:
但是我的代码返回这个:
我不知道我做错了什么,我尝试了另一个代码,它返回了所有字符,但我需要让一些字节返回一个点“。”,正如你在打印中看到的那样。
这是我的代码:
<?php
function hex2str($hex) {
$str = '';
for($i=0;$i<strlen($hex);$i+=2) $str .= chr(hexdec(substr($hex,$i,2)));
return $str;
} // i found this function on internet to convert HEX to String
$nome = "apateDNS.exe";//the name of the file
$arquivo = fopen($nome, "r");
$read = fread($arquivo,filesize($nome));
$hex = bin2hex($read);// return the hex of the binary
$hehe = chunk_split(strtoupper($hex), 2, " ");// split the hex each 2 bytes
$haha = str_split($hehe, 48); //split the hex each 48 characters (32 bytes + 16 blank spaces)
foreach($haha as $linha => $i){
echo "0000000".dechex($linha*16);
echo " ".$i." ".hex2str($i)."<br>";
}
?>
已解决:忘记删除函数中的空格...
$hex = str_replace(" ", "", $hex);
【问题讨论】:
-
你必须用点替换不可打印的字符:
-
您也可以将
echo "0000000".dechex($linha*16);替换为echo sprintf( '%08X', $linha*16 );。更漂亮!