How to Convert a Decimal into Hexadecimal with dechex

How to convert a decimal into hexadecimal with dechex

It's common when working with PHP that you'll need to make conversions between different numbering systems. Within computing, there are two main number systems used, decimal and hexadecimal. Others but less common in day-to-day codebases include binary and octal. These numbering systems are used for a variety of purposes, such as (but not a fully definitive list), representing colours, sorting data in memory, encoding data, or debugging. Knowing how to convert between the two systems makes it easier when creating web applications in PHP. Before we jump into exploring what decimal and hexadecimal numbering systems are, we first need to understand what "base" is.

What is 'Base' in Computing Number Systems?

The term "base" in computing numbering systems refers to the number of unique digits that are used in that numbering system. This unique number of digits per numbering system is what separates them apart from each other. The most common numbering systems are base 10 and base 16.

What is Decimal?

The decimal number system uses the base 10. This means that it can represent any number within 10 digits. These include any number from 0 - 9. Remember we always count zero in computing, and typically in PHP arrays we start from zero better known as zero-based. Because we have 10 digits, it means that each digit in the numbering system is a power of 10, from the rightmost digit. We do this because it represents a specific power of 10, at each step.

# 312 in decimal can be written as;
3 * 10^2 + 2 * 10^1 + 1 * 10^0 = 123

Using this method, allows us to easily calculate the values of a decimal number by adding up each digit's corresponding power of 10.

What is Hexadecimal?

Similar to decimal, the hexadecimal is a number system that uses the base 16, instead of 10. This means that there are more digits represented, in this case, 16 of them. Because it's zero-based, we start from 0 and then go up to 15 unique digits. But unlike decimal, hexadecimal also includes alpha characters. Hexadecimal numbering systems range from 0 to 9 (like decimal) then continue to include 6 more unique values which range from A to F. IF you're familiar with working with CSS in HTML documents, you would have come across hexadecimal because CSS uses hexadecimal to represent colour. Where the letters A to F are used they represent values from 10 to 15. So, "F" represents the value of 15 in hexadecimal, because it's the last letter in the A - F range, and we already know we're zero-based, so that's 15.

# 312 in decimal can be written as;
3 * 10^2 + 2 * 10^1 + 1 * 10^0 = 123

What is the dechex() PHP function?

Converting between decimal and hexadecimal can be quite a complex task if done manually, and prone to human error. Luckily PHP has a built-in function that allows you to convert between the two numbering systems. The PHP function "dechex" takes one argument, which is the decimal number you wish to convert, and returns a string that contains the hexadecimal representation of that number. We would also use "hexdec" to convert it back to its original number if we wanted to. This works the same way as dechex but works to convert the other way around. However, hexdec returns either an int float, not a string like the dechex function. On 32-bit platforms, the maximum number that can be converted is PHP_INT_MAX * 2 + 1, whereas 64-bit is much greater around (2^63 - 1).

dechex(decimal_number);

Let's explore why converting from decimal to hexadecimal might be useful in a few examples. Let's say you store colours in a database in decimal format, but for the web, we need to convert this into a hexadecimal value so it can be rendered correctly. The below example shows passing the decimal number "16777215" outputs "ffffff", which is a white colour used by computer software, such as a web browser.

# Outputs; ffffff
echo dechex('16777215');

Other similar PHP functions used to perform mathematical conversions include decbin and decoct. Where decbin is used to convert from decimal to binary (formation of 1's and 0's) and decoct is used to decimal to octal (following the base-8 numeral system).

Conclusion

Working with different number systems is a common task when working with PHP. Storing and using this different numbering system comes with the type of task at hand, and there will be times when using a base 10 approach may favour a base 16 approach. By using the dechex function, it will allow you to convert between the two number systems.

  • Decimal is base 10 - commonly used to store data such as a product's price
  • Hexadecimal is base 16 - commonly used to represent colours
  • The largest number that can be converted is determined by the platform's architecture and most importantly its memory limit

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all