Converts an integer into a hexadecimal string.
What Is dechex() in PHP?
Converts an integer into a hexadecimal string.
Its syntax is:
dechex(int $num): string
Parameters:
$num— Accepted as int; required argument.
Return type: string. It does not add a 0x prefix and negative values use the underlying integer representation.
Availability: Core PHP.
Example 1: Basic Usage
<?php
echo dechex(255);
Output:
ff
It does not add a 0x prefix and negative values use the underlying integer representation.
Example 2: A Different Input
<?php
echo dechex(4095);
Output:
fff
It does not add a 0x prefix and negative values use the underlying integer representation.
Practical Example: Applying It in a Program
<?php
$channels = [16, 128, 255]; foreach ($channels as $channel) { echo str_pad(dechex($channel), 2, "0", STR_PAD_LEFT) . PHP_EOL; }
Output:
10
80
ff
It does not add a 0x prefix and negative values use the underlying integer representation.
Important Behavior and Edge Cases
- Key behavior: It does not add a 0x prefix and negative values use the underlying integer representation.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
dechex() | Converts an integer into a hexadecimal string. |
hexdec() | hexdec() parses hexadecimal text as a number; dechex() turns an integer into hexadecimal text. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: It does not add a 0x prefix and negative values use the underlying integer representation | It does not add a 0x prefix and negative values use the underlying integer representation. |
Key Takeaways
- Converts an integer into a hexadecimal string.
- It does not add a 0x prefix and negative values use the underlying integer representation.
- Core PHP.
Summary: It does not add a 0x prefix and negative values use the underlying integer representation.