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.
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">'1A'</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.
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">'123'</span></span><span>, </span><span><span class="hljs-number">5</span></span>, </span><span><span class="hljs-string">'0'</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.
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">'1A'</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">'0'</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>
hexdec($hex) converts '1A' into decimal 26.
str_pad($decimal, 5, '0', STR_PAD_LEFT) pads 26 into a 5-digit string, using '0' on the left. The result is '00026'.
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">'1A'</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">'0'</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.
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.