【问题标题】:Convert HEX to ANSI in PHP [closed]在 PHP 中将 HEX 转换为 ANSI [关闭]
【发布时间】: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 );。更漂亮!

标签: php hex ansi


【解决方案1】:
function hex2str($hex) {
    $str = '';
    for($i=0;$i<strlen($hex);$i+=2) {
        $decValue = hexdec(substr($hex,$i,2));
        if($decValue < 32) {
            $str .= '.';
        } else {
            $str .= chr($decValue);
        }
    }
        return $str;
}

要生成所有十六进制代码及其翻译的表格,请使用以下代码:

for($x = 0; $x < 16; $x++) {
    $bin = $txt = array();
    for($y = 0; $y < 16; $y++) {        
        $num = dechex($x * 16 + $y);
        if(strlen($num) == 1) $num = '0' . $num;
        $bin[] = $num;
        $txt[] = hex2str($num);

    }
    echo (implode(' ',$bin) . '     ' . implode(' ',$txt)) . '<br/>';
}

【讨论】:

  • 很好,最大,这个函数将不可打印的字符替换为一个点,但它不像第一个屏幕截图,一些字节没有被转换。 “5A”应该是一个“Z”,但它变成了一个点……而且还有一些审讯。还有其他提示吗? Thxxx伙计们
  • 我添加了一些代码来生成所有十六进制代码及其字符表示形式的表格。能否提供输出截图?
  • 我忘记删除函数中的空格...这就是它不起作用的原因。这是截图:prntscr.com/93z9qw
猜你喜欢
  • 2014-01-13
  • 2014-04-17
  • 2019-07-16
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 2014-08-25
  • 2018-07-22
  • 1970-01-01
相关资源
最近更新 更多