1

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?

2
  • $hexString = "0x" . $value? Commented Jun 16, 2017 at 10:43
  • Can you please provide the input value? Commented Jun 16, 2017 at 10:53

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

Comments

0

Give it a try

  $value = dechex(bindec($msg));
     echo $value ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.