php字符串轉(zhuǎn)為16進(jìn)制的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后通過(guò)String2Hex方法將字符串轉(zhuǎn)為16進(jìn)制;最后通過(guò)return返回轉(zhuǎn)換結(jié)果即可。
推薦:《PHP視頻教程》
php字符串和16進(jìn)制編碼的相互轉(zhuǎn)換
php字符串是十進(jìn)制的
/** **字符串轉(zhuǎn)16進(jìn)制 **/ public function String2Hex($string){ $hex=''; for ($i=0; $i < strlen($string); $i++){ $hex .= dechex(ord($string[$i])); } return $hex; } /** **16進(jìn)制轉(zhuǎn)字符串 **/ public function Hex2String($hex){ $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2){ $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string; } // example: $hex = String2Hex("test sentence..."); // $hex contains 746573742073656e74656e63652e2e2e echo Hex2String($hex); // outputs: test sentenc
我在做aes加密是發(fā)現(xiàn)一個(gè)問(wèn)題,發(fā)現(xiàn)將字符串轉(zhuǎn)16進(jìn)制會(huì)出現(xiàn)轉(zhuǎn)出數(shù)據(jù)位數(shù)對(duì)不上的問(wèn)題可以修改為如下試試:
public function String2Hex($string){ $hex=''; // for ($i=0; $i < strlen($string); $i++){ // $hex .= dechex(ord($string[$i])); // } $hex = bin2hex($string); return $hex; }