note 100578 modified in function.hexdec by danbrown
| From: | danbrown@php.net | Date: | Mon, 25 Oct 2010 15:06:46 +0000 |
| Subject: | note 100578 modified in function.hexdec by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173442@lists.php.net to get a copy of this message | ||
Here's a short example to convert strings between hex and chars:
<?php
print hextostr("616E647940686176656C616E642E636F6D")."\n";
print strtohex("Knowledge is power")."\n";
function hextostr($x) {
$s='';
foreach(explode("\n",trim(chunk_split($x,2))) as $h) $s.=chr(hexdec($h));
return($s);
}
function strtohex($x) {
$s='';
foreach(str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
?>
--was--
Here's a short example to convert strings between hex and chars:
print hextostr("616E647940686176656C616E642E636F6D")."\n";
print strtohex("Knowledge is power")."\n";
function hextostr($x) {
$s='';
foreach(explode("\n",trim(chunk_split($x,2))) as $h) $s.=chr(hexdec($h));
return($s);
}
function strtohex($x) {
$s='';
foreach(str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
http://php.net/manual/en/function.hexdec.php