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

How to Use hexdec with str_pad to Convert Hexadecimal Numbers to 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 hexadecimal input doesn’t meet the expected length, requiring zero-padding to ensure fixed-length output. PHP provides powerful built-in functions hexdec and str_pad, which make this task simple. This article explains in detail how to combine these two functions to handle hexadecimal conversion and digit padding.

1. Introduction to 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 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 fills the missing part with the specified character, defaulting to spaces. We can use this 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 on the left side.

3. Combining hexdec and str_pad

Now, suppose we need to convert a hexadecimal number into decimal and ensure the output is of fixed length. For instance, we want to convert '1A' into a 5-digit decimal number, padding with zeros where necessary.

We can achieve this by combining hexdec with 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><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><span class="hljs-variable">$hex</span></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><span class="hljs-title function_ invoke__">str_pad</span></span><span>(</span><span><span class="hljs-variable">$decimal</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-comment">// Pad to fixed length</span></span><span>
<p></span>echo $fixedLengthDecimal;  // Output 00026<br>
</span>

4. Code Explanation

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

  2. str_pad($decimal, 5, '0', STR_PAD_LEFT) pads 26 into a 5-digit string, using '0' on the left. The result is '00026'.

5. Flexibility and Optimization

This approach allows flexibility in adjusting the output length. For example, we can change the output length to 8 digits or any desired size by modifying the length parameter in 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><span class="hljs-comment">// 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><span class="hljs-variable">$hex</span></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><span class="hljs-title function_ invoke__">str_pad</span></span><span>(</span><span><span class="hljs-variable">$decimal</span></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 $fixedLengthDecimal</span>;  // Output 00000026<br>
</span>

This makes it easy to adapt to different requirements by simply adjusting the parameter.

6. Conclusion

By combining hexdec and str_pad, we can conveniently convert hexadecimal numbers into fixed-length decimal numbers. hexdec handles the conversion, while str_pad ensures the output length is consistent. This way, whether dealing with small or large numbers, we can easily standardize the output with zero-padding where necessary.