Req #69947 [Opn->Nab]: hex2bin: allow odd-length string
| From: | nikic@php.net | Date: | Fri, 26 Jun 2015 21:48:35 +0000 |
| Subject: | Req #69947 [Opn->Nab]: hex2bin: allow odd-length string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193929@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69947&edit=1
ID: 69947
Updated by: nikic@php.net
Reported by: marc at mabe dot berlin
Summary: hex2bin: allow odd-length string
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: *General Issues
PHP Version: 7.0Git-2015-06-26 (Git)
Block user comment: N
Private report: N
New Comment:
Please see the CAUTION note on http://php.net/hex2bin. hex2bin
does NOT convert a hexadecimal number to a binary number. It decodes a hexadecimally encoded binary
strings. Stripping a leading 0 would corrupt the whole string.
Previous Comments:
------------------------------------------------------------------------
[2015-06-26 21:24:47] marc at mabe dot berlin
Description:
------------
The current behavior on converting a hexadecimal string with an odd-length into binary data results
into a warning and returns false.
As the input is in hexadecimal notation it should be very valid to remove "0" from the
left.
This behavior is really annoying as it's not enough the check for valid characters it's
required to check the length, too.
Additionally within PHP itself the behavior is inconsistent:
bin2hex(hex2bin("123")) = Warning: hex2bin(): Hexadecimal input string must have an
even length
dechex(0x123) = "123"
base_convert(base_convert("123", 16, 2), 2, 16) = "123"
bin2hex(pack('H*', '123')) = "1230" (pack() will be part of another
bug report)
And on looking into JS and (My)SQL it's working well and can result in an odd-length:
JavaScript:
parseInt("123", 16).toString(16) = "123"
MySQL:
SELECT HEX(UNHEX('123')) = "0123"
SELECT HEX(0x123) = "0123"
I have also found #61660 & #65155 where bin2hex(hex2bin("123")) was resulting into
"12" or false without any notice which was surly wrong but simply adding a warning and
returning false isn't a good fix in my opinion.
Test script:
---------------
bin2hex(hex2bin("123"))
Expected result:
----------------
"0123"
Actual result:
--------------
Warning: hex2bin(): Hexadecimal input string must have an even length
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69947&edit=1