php 列印出字串的16進位_PHP教程

來源:互聯網
上載者:User
下面這個函數是一個php 列印出字串的16進位執行個體,這裡面的核心函數就是 chr擷取二進位然後再進行轉成16進位數。
代碼如下 複製代碼

/*
php 列印出字串的16進位資料
*/
function hex_dump($data, $newline="n")
{
static $from = '';
static $to = '';

static $width = 16; # number of bytes per line

static $pad = '.'; # padding for non-visible characters

if ($from==='')
{
for ($i=0; $i<=0xFF; $i++)
{
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
}

$hex = str_split(bin2hex($data), $width*2);
$chars = str_split(strtr($data, $from, $to), $width);

$offset = 0;
foreach ($hex as $i => $line)
{
echo sprintf('%6X',$offset).' : '.implode(' ', str_split($line,2)) . ' [' . $chars[$i] . ']' . $newline;
$offset += $width;
}
}

$info="this is a testx00x99hex_dump";
print_r(hex_dump($info));
/*
輸出結果:

0 : 74 68 69 73 20 69 73 20 61 20 74 65 73 74 00 99 [this is a test..]

10 : 68 65 78 5f 64 75 6d 70 [hex_dump]
*/
?>


http://www.bkjia.com/PHPjc/633110.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633110.htmlTechArticle下面這個函數是一個php 列印出字串的16進位執行個體,這裡面的核心函數就是 chr擷取二進位然後再進行轉成16進位數。 代碼如下 複製代碼 ?...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.