Skip to content
Hex Calculator

PHP Integer to Hex Converter

Convert a PHP integer to its hexadecimal string form — the exact output PHP's dechex() function returns, including how it handles negative integers.

Hex value
0xFF
Swapnil Sanghvi

Built by

Swapnil Sanghvi

Full-Stack Web Developer & WordPress Developer

Swapnil Sanghvi is a full-stack web and WordPress developer, UI designer, and full-time freelancer who builds and maintains Hex Calculator.

PHP Integer to Hex Converter tool preview card from Hex Calculator
Share preview: this is the card that appears when you share the PHP Integer to Hex Converter page on social media.

Further reading: hex — Stack Overflow

How PHP Converts an Integer to Hex

dechex() converts a decimal integer to its base-16 string form, using lowercase digits with no prefix and no padding by default — combine with str_pad() for fixed-width, zero-padded output.

Where PHP Integer to Hex Actually Comes Up

Generating a Hex Color String

Converting individual RGB integer values to their 2-digit hex form and concatenating them to build a hex color string dynamically.

str_pad(dechex($r), 2, "0", STR_PAD_LEFT)

Formatting an ID or Hash as Hex

Converting a numeric ID or computed hash value to hex for display, logging, or building a compact identifier string.

dechex($id) -> compact hex identifier

Comparing Against a Known Hex Magic Number

Converting a value read from a file to hex to compare it (or log it) against a documented magic number, when debugging a binary file parser.

dechex($bytes) vs. documented magic number

Why Use This Calculator Instead of Doing It by Hand

  • Matches the digit output of PHP's dechex() function
  • Runs entirely in your browser — nothing you type gets sent anywhere
  • Saves manually converting and formatting the output
  • Useful for checking expected output before writing test assertions

Limitations

  • Adds a 0x prefix and uses uppercase digits — PHP's dechex() returns lowercase digits with no prefix.
  • Doesn't zero-pad to a fixed width — combine with str_pad() yourself if you need that.

Frequently Asked Questions

How do I convert an integer to hex in PHP?

Use the built-in dechex() function — dechex(255) returns "ff", lowercase with no prefix.

How do I pad the result to a fixed width?

Combine dechex() with str_pad(), like str_pad(dechex($value), 8, "0", STR_PAD_LEFT), to get a fixed-width, zero-padded hex string.

What does dechex() do with a negative number?

It treats the value as an unsigned integer at PHP's native int width (64-bit on virtually all modern platforms), so a negative number produces a very long hex string rather than a minus sign.

How do I convert a hex string back to an int in PHP?

Use hexdec() — the counterpart to dechex() for parsing a hex string back into an integer (or float, if the value exceeds PHP_INT_MAX).