Is there any way to compare HEX values in PHP?? Here's a snipet of my code:

$hexCode = dechex(ord(" "));
if ($hexCode == 0x20)
  print "Found a space";
else {
  print "not a space";
}

The == part is not working. Is this the proper way to compare HEX values??

Thanks in advance.

    Dont use dechex() here since it returns a string. Just compare the return value of ord() with the integer.

      Alright, I have tried that and it works. I would still like to know the proper syntax for comparing hex values, though.

        $hexCode == 0x20

        This code doesn't check the right hex value. 0x20 hex is decimal 32, which is ascii " " (space).... however, the comparison line returns true for decimal 50, which is the number 2. It's comparing the string value, like returning true for hex value 32 which is not what I want. Is there any way to compare the hex values ?

          I would still like to know the proper syntax for comparing hex values, though.

          In a way, there's no such thing as a hex value. 0x20 is 32 in hexadecimal representation, but whether it be in decimal, octal or hexadecimal representation, it is still the same number.

          dechex() takes an integer and returns a string of the hexadecimal representation of the number. This is not a numeric value, but a string value. Now, PHP can automagically convert between these types, but dechex() does not include the hexadecimal notation prefix (0x) in the return value.

          So if you do want to compare, you would use:
          dechex(ord(" ")) == 20
          But this is obviously going to be confusing, since you actually mean:
          dechex(ord(" ")) == "20"

          Hence there's no point using dechex() in the first place when you intend to compare with an integer.

            Write a Reply...