字符串轉十六進制

<?php
    if(isset($_POST['Submit']))
    {
        $str = $_POST['str'];
        $split = $_POST['split'];
        $html = strToHex($str,TRUE,$split);
    }
   
//漢字轉換爲16進制編碼
function strToHex($s,$strCase=FALSE,$split='') 
    {
        $r = "";  //定義個變量保存結果

        $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

        for ($i=0; $i<strlen($s); $i++) 
        {
            //(ord($s{$i}) >> 4) 表示轉換爲對應的十進制數據,然後除以16
            $r .= $split.($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
        }
        if($strCase)
        {
            $r = strtoupper($r);
        }
        return $r;    
    }
?>
<html>
    <head>
        <title>字符串轉十六進制</title>
    </head>
    <body>
    <center>
        <form action="" method="post">
            分割字符<input type="text" name="split" value="%" /><br />
            輸入字符<input type="text" name="str" /><br />
            <input type="submit" name="Submit" value="提交" />
        </form>
        對應的十六進制 <?php echo $html; ?>
    </center>
    </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章