I need to take some binary data and convert it to hex. When I do
$value = bin2hex($msg);
$value is "0003". However I need it to be a hex string, like 0x0003 or even just "03" (hex), in order to use it.
How can I convert it?
I thing you need to convert a string to hex string to use bin2hex($msg);
You can use this
function strToHex($string){
$hex = '';
for ($i=0; $i<strlen($string); $i++){
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2);
}
return strToUpper($hex);}
Like This
$value = bin2hex(strToHex($msg));
$hexString = "0x" . $value?