十六进制和反面的编码/解码字符串 [英] Encoding/decoding string in hexadecimal and back
本文介绍了十六进制和反面的编码/解码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个可能包含任何字符(包括unicode字符)的字符串,我如何将该字符串转换为十六进制表示形式,然后反转并从十六进制获取此字符串?
Given a string that may contain any character (including a unicode characters), how can I convert this string into hexadecimal representation, and then reverse and obtain from hexadecimal this string?
推荐答案
使用pack()
和unpack()
:
function hex2str( $hex ) {
return pack('H*', $hex);
}
function str2hex( $str ) {
return array_shift( unpack('H*', $str) );
}
$txt = 'This is test';
$hex = str2hex( $txt );
$str = hex2str( $hex );
echo "{$txt} => {$hex} => {$str}\n";
会产生
这是测试=> 546869732069732074657374 =>这是测试
This is test => 546869732069732074657374 => This is test
这篇关于十六进制和反面的编码/解码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文