Anonymous 发表于 2021-6-16 22:22:57

汉字转为ASCII码和ASCII码转换为汉字

字符串转为ASCII/**
* 字符串转为ASCII
*
* @param string $str 需要转换的字符串
*
* @return string $asc ASCII码
*/
function ascii_encode($str)
{
    $str = mb_convert_encoding($str, 'GBK');
    $asc = '';
    for ($i = 0; $i < strlen($str); $i++) {
      $temp_str = dechex(ord($str[$i]));
      $asc .= $temp_str.$temp_str;
    }
    return strtoupper($asc);
}ASCII转字符串
/**
* ASCII转为字符串
*
* @param string $ascii ascii
*
* @return string $str 字符串
*/
function ascii_decode($sacii)
{
    $asc = str_split(strtolower($sacii), 2);
    $str ='';
    for ($i = 0; $i < count($asc); $i++) {
      $str.= chr(hexdec($asc[$i].$asc[$i]));
    }
    return mb_convert_encoding($str, 'UTF-8', 'GBK');
}


页: [1]
查看完整版本: 汉字转为ASCII码和ASCII码转换为汉字