bin2hex
-
The
bin2hexfunction in PHP converts a string containing binary data into a human-readable string containing the hexadecimal representation of the data.PHP
$bytes = chr(0x00) . chr(0xFF); $hexString = bin2hex($bytes); var_export($hexString); //=> "00ff"
Ruby does not have an equivalent method but
String#unpackwith a format string ofH*will accomplish the same task.Ruby
bytes = 0x00.chr + 0xFF.chr hex_string = bytes.unpack('H*').first p hex_string #=> "00ff"
String#unpackalways returns an array of extracted values. In the example above, we need to take thefirst(and only) element of the result to arrive at the same string as produced by PHP’sbin2hex.see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

