PHP code to convert string to hex value.

Encode & Decode a String to Hex Value.


code : 
<?php
function strToHex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}


function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
$value= "Praneeth Damera MCA";
echo "<br>String Before encode: ".$value;
$encodedvalue = strToHex($value);
echo "\n<br>encode to Hex Value : ".$encodedvalue;
$decodedvalue = hexToStr($encodedvalue);
echo "\n<br>After Decode Value : ".$decodedvalue;
?>

out put:

String Before encode: Praneeth Damera MCA
encode to Hex Value : 5072616e656574682044616d657261204d4341
After Decode Value : Praneeth Damera MCA

Comments