DevKB
Web Development Knowledge Base
HOME | TOP 20 | WRITE AN ARTICLE |
Sections :

RSS RSS Feed

Vous aussi, aidez les autres développeurs, publiez vos bouts de codes utiles et vos liens préférés ...
Publiez un article !


Valid HTML 4.0 Transitional


Olivier Ligny - - 09/04/2008 - vue 50 fois

str2hex() / hex2str() en PHP pour encoder/décoder du texte en hexadécimal

Voici deux fonctions PHP pour encoder/décoder du texte en hexadécimal :

function str2hex($string) {
  $hex = "";
  for ($i = 0; $i < strlen($string); $i++) {
    $hex .= (strlen(dechex(ord($string[$i]))) < 2) ?
    "0" . dechex(ord($string[$i])) : dechex(ord($string[$i]));
  }
  return $hex;
}

function hex2str($hex) {
    $str = '';
    for($i=0;$i < strlen($hex);$i+=2) {
      $str.=chr(hexdec(substr($hex,$i,2)));
    }
    return $str;
}

 

 




Write a comment :
Your name :     E-mail (optional) :

AntiSpam : please write the sum of 4 + 1 =