Bug #61660 [Opn]: bin2hex(hex2bin($data)) != $data

From: Date: Sat, 07 Apr 2012 17:04:25 +0000
Subject: Bug #61660 [Opn]: bin2hex(hex2bin($data)) != $data
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-8214@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61660&edit=1 ID: 61660 Updated by: laruence@php.net Reported by: krtek4+php at gmail dot com Summary: bin2hex(hex2bin($data)) != $data Status: Open Type: Bug -Package: Unknown/Other Function +Package: Documentation problem Operating System: Debian Linux PHP Version: 5.4.1RC1 Block user comment: N Private report: N New Comment: actually, I think I can not make a good patch for this, since 0* will be considerd as a oct number... and if I pad some magic number like '0f' to it, there will be a mess if you pass the result to somewhere not bin2hex.. so, mark this as doc problem Previous Comments: ------------------------------------------------------------------------ [2012-04-07 16:46:29] laruence@php.net I think it's better to well document this. or, add a prepend '0' , I will make a patch for this. ------------------------------------------------------------------------ [2012-04-07 16:45:45] krtek4+php at gmail dot com I'm aware that this is a problem with the internal reprensation of the binary value which has to be aligned on 8 bits. But with the actual implementation, we are losing informations. A possible solution would be to pad the binary data with 0 on the left and when converting back again to hex, remove the leading 0s. At least, something should be said in the documentation about this shortcoming. ------------------------------------------------------------------------ [2012-04-07 15:46:35] krtek4+php at gmail dot com Description: ------------ If you try to apply bin2hex on the result of hex2bin, when the length of the initial data is an odd number, the resulting data are not the same as the original. $ php -v PHP 5.4.1RC1 (cli) (built: Apr 6 2012 13:31:16) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies This is the original Debian package present in sid on the 7th April 2012 Test script: --------------- <?php $data = '12345'; var_dump($data, bin2hex(hex2bin($data))); // outputs : // string(5) "12345" // string(4) "1234" Expected result: ---------------- The output should be the same. (ie 12345 both times) Actual result: -------------- string(5) "12345" string(4) "1234" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=61660&edit=1

« previous php.doc.bugs (#8214) next »