Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongster/27e9b7d860fa34b306fb to your computer and use it in GitHub Desktop.
Save hongster/27e9b7d860fa34b306fb to your computer and use it in GitHub Desktop.
PHP encodes string in UTF-8. This code converts it to Unicode, and then represent it as hexadecimal.
<?php
function strToHex($string){
$hex = '';
for ($i=0; $i<strlen($string); $i++){
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2);
}
return $hex;
}
$s = '深圳市南山区南海大道1079号 http://www.gps.com/map.aspx?lat=23.123&lng=113.123';
$s = mb_convert_encoding($s, 'Unicode', 'UTF-8');
echo strToHex($s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment