hexdec only accepts hexadecimal numbers in the form of strings as input. If a non-string type is passed in, PHP will convert automatically first, but this may cause an exception to the result. For example:
<?php
// Correct usage,String input
echo hexdec("1a"); // Output 26
// Digital input,Automatically convert to string "26"
echo hexdec(26); // Output 38,because 26 Will be treated as a string "26" deal with
Pay attention to the misunderstandings that this automatic type conversion may cause. Confirm the type of input parameters is the first step in debugging.
Many times, hexdec returns a result that does not meet the expectations because the input itself is incorrect. At this time, you can print data before calling and confirm the input content:
<?php
$hexString = "1g"; // Illegal characters g
if (!ctype_xdigit($hexString)) {
echo "The input string contains non-hexadecimal characters: $hexString\n";
}
echo hexdec($hexString); // Output 1,because遇到Illegal characters后停止转换
It is a good habit to use the ctype_xdigit() function to detect whether a string is legal in hexadecimal.
If you use modern IDEs like PhpStorm, it is recommended to set breakpoints, step-by-step debugging of the program, and observe the changes before and after the variable is passed into hexdec . Real-time monitoring of variable types and values can help you discover unexpected inputs or data contamination.
Although hexdec does not throw exceptions, you can use PHP's error log and exception handling mechanism to write some security code around the call point, such as:
<?php
set_error_handler(function($errno, $errstr) {
echo "Error capture: [$errno] $errstr\n";
});
$input = "zz123";
if (!ctype_xdigit($input)) {
trigger_error("Enter a non-hexadecimal string: $input", E_USER_WARNING);
}
echo hexdec($input);
The log discovery of input exceptions helps locate the call chain.
Encapsulate the hexdec call into a function, design unit test cases for it, cover normal, boundary and exception inputs, and ensure that the function is robust:
<?php
function safeHexDec(string $hex) : int {
if (!ctype_xdigit($hex)) {
throw new InvalidArgumentException("Illegal hexadecimal string: $hex");
}
return hexdec($hex);
}
Then write the test with PHPUnit:
<?php
use PHPUnit\Framework\TestCase;
class HexDecTest extends TestCase {
public function testValidHex() {
$this->assertEquals(26, safeHexDec("1a"));
}
public function testInvalidHex() {
$this->expectException(InvalidArgumentException::class);
safeHexDec("1g");
}
}
Sometimes you can use online tools or command line printf to perform quick verification of hexadecimal to decimal to ensure that the results of PHP output are accurate.
Through the above steps, you can quickly locate problems in hexdec calls and avoid troubles caused by implicit type conversion, illegal input or wrong logic. Keeping input checking, log capture and unit testing in mind is the key to improving code quality and debugging efficiency.
<?php
// Example:Safe call hexdec Functions of
function safeHexDec(string $hex): int {
if (!ctype_xdigit($hex)) {
throw new InvalidArgumentException("Illegal hexadecimal string: $hex");
}
return hexdec($hex);
}
try {
$value = safeHexDec("1a3f");
echo "Convert result: $value\n";
} catch (InvalidArgumentException $e) {
echo "mistake: " . $e->getMessage() . "\n";
}
?>