Skip to content
Advertisement

PHP Function hex2bin() doesn’t return binary strings

The php manual says that hex2bin() returns a string with a binary representation.

I have this code:

$hex = hex2bin("6578616d706c65206865782064617461");
var_dump($hex);

The output is:

string 'example hex data' (length=16)

Pardon me if I’m wrong but I believe that the output isn’t a binary string?? Did the manual made an error, or am I missing something?

————-edit————

Is 'example hex data' a binary representation of data?

Advertisement

Answer

hex2bin turns hexadecimal numbers into raw bytes. These raw bytes output to the screen will be interpreted by your browser and/or CLI, which will turn it into text. bin2hex does not return a string like “01001000101” if that’s what you expected; that’d be an ASCII representation of a binary string, not a binary string. See for example this if that’s what you want: How to see binary representation of variable

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement