二进制转换,PHP字符串(含中文)转二进制串代码
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function StrToBin($str){ $arr = preg_split('/(?<!^)(?!$)/u', $str); foreach($arr as &$v){ $temp = unpack('H*', $v); $v = base_convert($temp[1], 16, 2); while($v<8)$v='0'.$v; unset($temp); } return join(' ',$arr); } function BinToStr($str){ $arr = explode(' ', $str); foreach($arr as &$v){ $v = pack("H".strlen(base_convert($v, 2, 16)), base_convert($v, 2, 16)); } return join('', $arr); } echo StrToBin("织梦先生"); echo '<br>'; echo BinToStr('111001101001110010001001 111001001011100010000000 111010011010001010010111 111001111000100110011011 111010011000000010111100 111001111001101010000100 111001011011111110000011 111011111011110010001100 111001001011110110100000 111001011011000010110001 111001011000111110101111 111001001011101110100101 111001111000100110011011 111010011000000010111100 111001001011101010001110 111001011010010010101001 111001001011100010001011 111011111011110010000001'); |
代码解释: Continue reading