note 80432 deleted from function.bin2hex by danbrown
| From: | danbrown@php.net | Date: | Thu, 19 Nov 2009 21:59:00 +0000 |
| Subject: | note 80432 deleted from function.bin2hex by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163114@lists.php.net to get a copy of this message | ||
Note Submitter: PHPcoder at cyberpimp dot t28 dot net
----
Re. Matt W. and bin2hex vs. hexdec...
The compliment of bin2hex is actually pack('H*', $hex_str), where $hex_str is the ASCII
hexadecimal string (ordered high nibble first) to convert to an 8-bit (binary-safe) STRING of data.
hexdec, as explained in the documentation, converts a hexadecimal value to a decimal number. It
returns a PHP NUMERIC value, not a string!
So just a quick recap:
For STRING conversions...
... bin2hex($any_str) converts an 8-bit (binary-safe) string of data to an ASCII hex string.
... pack('H*', $hex_str) converts an ASCII hex string to an 8-bit (binary-safe) string
of data.
For NUMERIC conversions...
... hexdec($hex) converts an ASCII string representation of a number in hex to a PHP numeric
value (I'm guessing integer).
... dechex($int) converts an integer to an ASCII string representation of the value in hex.
... bindec($str) converts a string of ASCII 0's and 1's (representing a binary value)
to a PHP numeric value (I'm guessing integer).
... decbin($int) converts an integer to a string of ASCII 0's and 1's representing the
value in binary.
... octdec($str) converts an ASCII string representation of a number in octal to a PHP numeric
value (I'm guessing integer).
... decoct($int) converts an integer to an ASCII string representation of the value in octal.
And of course as every PHP programmer should know, PHP internally handles type conversion between
ASCII string representations of base-10 numbers and PHP numeric values (integer, float, etc.).
Be wary of your data types! Binary data strings are not the same as PHP numeric values!