Current Location: Home> Latest Articles> How to Use hexdec with str_pad to Convert Hexadecimal Numbers of Insufficient Digits into Fixed-Length Decimal?

How to Use hexdec with str_pad to Convert Hexadecimal Numbers of Insufficient Digits into Fixed-Length Decimal?

gitbox 2025-09-02

In programming, it’s common to encounter scenarios where hexadecimal numbers need to be converted into decimal. Sometimes, the length of the hexadecimal string doesn’t match expectations, requiring zero-padding to ensure fixed-length output. PHP provides two powerful built-in functions, hexdec and str_pad, that make this task easy. This article will explain in detail how to use these two functions together to perform hexadecimal-to-decimal conversion with digit padding.

1. Introduction to the hexdec Function

hexdec is a PHP function that converts a hexadecimal string into a decimal integer. It takes a hexadecimal string as input and returns the corresponding decimal value.

<span><span><span class="hljs-variable">$decimal</span></span><span> = </span><span><span class="hljs-title function_ invoke__">hexdec</span></span><span>(</span><span><span class="hljs-string">&#039;1A&#039;</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$decimal</span></span><span>;  </span><span><span class="hljs-comment">// Output 26</span></span><span>
</span></span>

As shown above, hexdec('1A') converts the hexadecimal '1A' into the decimal 26.

2. Introduction to the str_pad Function

The str_pad function is used to pad a string to a specified length. If the string is shorter than the target length, it will be padded with the specified character (default is a space). We can use it to ensure a fixed-length numeric output.

<span><span><span class="hljs-variable">$padded</span></span><span> = </span><span><span class="hljs-title function_ invoke__">str_pad</span></span><span>(</span><span><span class="hljs-string">&#039;123&#039;</span></span><span>, </span><span><span class="hljs-number">5</span></span>, </span><span><span class="hljs-string">&#039;0&#039;</span></span>, STR_PAD_LEFT);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$padded</span></span><span>;  </span><span><span class="hljs-comment">// Output 00123</span></span><span>
</span></span>

In this example, str_pad pads the string '123' to a length of 5 using zeros, adding them to the left of the string.

3. Combining hexdec and str_pad to Meet Requirements

Now let’s say we need to convert a hexadecimal number to decimal while ensuring the output has a fixed length. For example, we want the hexadecimal number '1A' to be converted into a 5-digit decimal number, padding with zeros if necessary.

We can achieve this by combining hexdec and str_pad:

<span><span><span class="hljs-variable">$hex</span></span><span> = </span><span><span class="hljs-string">&#039;1A&#039;</span></span>;  </span><span><span class="hljs-comment">// A hexadecimal number</span></span><span>
</span><span><span class="hljs-variable">$decimal</span></span><span> = </span><span><span class="hljs-title function_ invoke__">hexdec</span></span>(</span><span><span class="hljs-variable">$hex</span></span>);  </span><span><span class="hljs-comment">// Convert to decimal</span></span><span>
</span><span><span class="hljs-variable">$fixedLengthDecimal</span></span> = </span><span><span class="hljs-title function_ invoke__">str_pad</span></span>(</span><span><span class="hljs-variable">$decimal</span></span>, </span><span><span class="hljs-number">5</span></span>, </span><span><span class="hljs-string">&#039;0&#039;</span></span>, STR_PAD_LEFT);  </span><span><span class="hljs-comment">// Pad to fixed length</span></span><span>
<p></span>echo </span>$fixedLengthDecimal</span>;  </span>// Output 00026<br>
</span>

4. Code Explanation

  1. hexdec($hex) converts '1A' into the decimal number 26.

  2. str_pad($decimal, 5, '0', STR_PAD_LEFT) pads 26 into a 5-character string, filling with '0' on the left. Thus, the output is '00026'.

5. Flexibility and Optimization

This method allows us to flexibly adjust the output length. For example, we can change the output length to 8 digits, or any length required, simply by modifying the length parameter in str_pad:

<span><span><span class="hljs-variable">$hex</span></span> = </span><span><span class="hljs-string">&#039;1A&#039;</span></span>;  </span><span><span class="hljs-comment">// Hexadecimal number</span></span><span>
</span><span><span class="hljs-variable">$decimal</span></span> = </span><span><span class="hljs-title function_ invoke__">hexdec</span></span>(</span><span><span class="hljs-variable">$hex</span></span>);  </span><span><span class="hljs-comment">// Convert to decimal</span></span><span>
</span><span><span class="hljs-variable">$fixedLengthDecimal</span></span> = </span><span><span class="hljs-title function_ invoke__">str_pad</span></span>(</span><span><span class="hljs-variable">$decimal</span></span>, </span><span><span class="hljs-number">8</span></span>, </span><span><span class="hljs-string">&#039;0&#039;</span></span>, STR_PAD_LEFT);  </span><span><span class="hljs-comment">// Pad to 8 digits</span></span><span>
<p></span>echo </span>$fixedLengthDecimal</span>;  </span>// Output 00000026<br>
</span>

This way, different requirements can be met with simple parameter adjustments.

6. Conclusion

By combining hexdec and str_pad, we can easily convert hexadecimal numbers into fixed-length decimal numbers. hexdec handles the conversion, while str_pad ensures the output meets the required digit length. Whether dealing with small or large numbers, this method allows for effortless zero-padding, ensuring standardized output.