php實現(xiàn)將字符串轉(zhuǎn)為ASCII的方法:【public function strtoascii($str{ $str=mb_convert_encoding($str,'GB2312');$change_after='';for(…】。
本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。
字符串與ASCII之間的轉(zhuǎn)換:
下面的函數(shù)是已經(jīng)封裝好的,可以直接拿來使用。
1、將字符串(中文同樣實用)轉(zhuǎn)為ascii(注意:我默認當前我們的php文件環(huán)境是UTF-8,如果是GBK的話mb_convert_encoding操作就不需要)
public function strtoascii($str){ $str=mb_convert_encoding($str,'GB2312'); $change_after=''; for($i=0;$i<strlen($str);$i++){ $temp_str=dechex(ord($str[$i])); $change_after.=$temp_str[1].$temp_str[0]; } return strtoupper($change_after); }
2、將ascii轉(zhuǎn)為字符串(中文同樣實用)(注意:我默認當前我們的php文件環(huán)境是UTF-8,如果是GBK的話mb_convert_encoding操作就不需要)
public function asciitostr($sacii){ $asc_arr= str_split(strtolower($sacii),2); $str=''; for($i=0;$i<count($asc_arr);$i++){ $str.=chr(hexdec($asc_arr[$i][1].$asc_arr[$i][0])); } return mb_convert_encoding($str,'UTF-8','GB2312'); }
推薦學習:php培訓