I can see how you might refer to the
0x prefix as a "sign". It signifies that the following digits are hexadecimal. But in programming, we almost universally use the word "sign" to refer to something that indicates whether a number is positive or negative. In a stored numeric value, there's usually one bit that indicates whether a number is negative. In program text, we can write
+42 to denote a positive integer and
-42 to denote a negative integer; the
- character is the sign. (I'm glossing over some details regarding 2's-complement and other representations, and issues regarding
- are really operators, not part of the integer constants. Don't worry about that for now.)
So referring to anything other than a positive-or-negative indicator as a "sign" is bound to cause confusion. The
0x in a hexadecimal literal like
0xfa0 can sensibly be referred to as a prefix, not as a "sign".
The name of PHP's
dechex function, documented here, is actually a bit misleading. It takes an unsigned integer argument and returns a string containing the human-readable hexadecimal representation of that argument. The argument is not decimal; it's almost certainly stored in binary. We think of integers as decimal because we usually write them that way, but in fact integer values can be written in decimal, octal, hexadecimal, or as complicated expressions -- all of which result in a binary stored value. It doesn't convert decimal to hexadecimal; it converts a number to hexadecimal -- a number that may have resulted from the conversion of a decimal literal in the PHP source code to a stored binary integer.
As for why the result of
dechex() doesn't include the
0x prefix, the documentation doesn't say, but it's probably just because it's more convenient that way. If you want a string with a
0x prefix, it's trivially easy to add it yourself (and to decide whether you want
dechex() returned a string with the prefix, and you didn't want it, you'd have to remove it.
This small program demonstrates these points:
// Various arguments to dechex (decimal, octal, hexadecimal):
echo "dechex(12345678) = \"", dechex(12345678), "\"\n";
echo "dechex(012345678) = \"", dechex(012345678), "\"\n";
echo "dechex(0xDEADBEEF) = \"", dechex(0xdeadbeef), "\"\n";
echo "dechex(4*5) = \"", dechex(4*5), "\"\n";
echo "With a prefix: \"0x", dechex(4*5), "\"\n";
// Another approach using sprintf():
echo sprintf("0x%x", 1234), "\n";
// ... or printf:
Here's the output:
dechex(12345678) = "bc614e"
dechex(012345678) = "53977"
dechex(0xDEADBEEF) = "deadbeef"
dechex(4*5) = "14"
With a prefix: "0x14"