php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26702 bin2hex compare with HEX value that equals DEC value of HEX of bin returns true
Submitted: 2003-12-22 23:24 UTC Modified: 2003-12-23 04:29 UTC
From: bugs dot php dot net at zetafleet dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.3 OS: Win32; Cygwin x86
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bugs dot php dot net at zetafleet dot com
New email:
PHP Version: OS:

 

 [2003-12-22 23:24 UTC] bugs dot php dot net at zetafleet dot com
Description:
------------
This is complicated, so I'll try explaining as best I can.

There is a binary file. The first byte is 0x44. Doing a comparison (==) to 0x2C returns "TRUE" because 44 is the decimal for 0x2C. The decimal value of 0x44 is 68. This should return "FALSE" but does not.

Reproduce code:
---------------
(bin2hex("D") == 0x2C) RETURNS TRUE;  SHOULD RETURN FALSE;
(bin2hex("D") == 0x44) RETURNS FALSE; SHOULD RETURN TRUE;
(bin2hex("D") == 44)   RETURNS TRUE;  SHOULD RETURN TRUE;

Expected result:
----------------
print(bin2hex("D") == 0x2C) //prints nothing
print(bin2hex("D") == 0x44) //prints '1'
print(bin2hex("D") == 44)   //prints '1'

Actual result:
--------------
print(bin2hex("D") == 0x2C) //prints '1'
print(bin2hex("D") == 0x44) //prints nothing
print(bin2hex("D") == 44)   //prints '1'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-23 04:29 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The problem here is that bin2hey returns a string without the \'0x\' to identify a hex number. Hence the string is interpreted as a decimal number (if possible) when compared to any number. 

To demonstrate:
$ php -r \'var_dump((\"0x\".bin2hex(\"D\")) == 0x44);\'
bool(true)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC