Search code examples
phpbinaryhexsubstringbin2hex

hex value to string of characters


I have a hex number that I get from a bin2hex() call. I need a string representing this number in the format outlined below:

$tmp = bin2hex($foo)
// $tmp is 0x123efd

I need some code that will give me the string "123efd".


Solution

  • It is a prefix of hexadecimal (0x). You need to remove the prefix with

    $tmp = substr(bin2hex($foo), 2); // $tmp is 123efd
    //assume that prefix is only 2 digits and you always remove the 2 digits