字符串转十六进制

<?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>

你可能感兴趣的:(字符串转十六进制)