In the process of converting hexadecimal strings to decimal integers, PHP and JavaScript provide their own standard functions: hexdec() in PHP and parseInt() in JavaScript. Although their basic functions are similar, there are some notable differences in specific implementations and boundary behaviors, which will be compared and illustrated in this article in detail.
Hexdec() is used to convert a hex string to a decimal integer, and its syntax is very simple:
<?php
echo hexdec("1A"); // Output 26
?>
The input to this function is a legal hexadecimal string (can contain 0x prefix or not), the return value is an integer or a floating point number when out of range.
JavaScript's parseInt() is more general, it can parse arbitrary strings, and the syntax is as follows:
console.log(parseInt("1A", 16)); // Output 26
The second parameter must be specified explicitly to indicate that this is a hexadecimal string. If radix is not specified, parseInt will try to automatically determine the binary based on the string format (which may cause errors).
There is a significant difference in the processing behavior of these two functions when encountering illegal characters.
<?php
echo hexdec("1A*F"); // Output 26
?>
PHP's hexdec() parses from the beginning of the string. Once an illegal character (such as * ) is encountered, it stops processing, but will not report an error, but returns the value that has been successfully parsed so far.
console.log(parseInt("1A*F", 16)); // Output 26
JavaScript behaves similarly to PHP, and it stops when the first illegal character is encountered and returns the previously successfully parsed part.
The two behave consistently in this regard and do not throw exceptions, but PHP has a slightly higher tolerance for errors, such as:
<?php
echo hexdec("GHI"); // Output 0
?>
And in JavaScript:
console.log(parseInt("GHI", 16)); // Output NaN
PHP returns 0 , while JavaScript returns NaN , which may lead to a difference in logical judgment.
Both PHP and JavaScript are case-insensitive when dealing with hexadecimal characters.
<?php
echo hexdec("1a"); // Output 26
?>
console.log(parseInt("1a", 16)); // Output 26
This provides consistency when developing across languages and does not require additional conversions.
When passing in a very large hexadecimal string, the two languages handle it differently.
In PHP:
<?php
echo hexdec("FFFFFFFFFFFFFFFF"); // Output 1.84467440737096E+19(Floating point number)
?>
In JavaScript:
console.log(parseInt("FFFFFFFFFFFFFFFF", 16)); // Output 18446744073709552000(Number,Accuracy may be lossy)
The parseInt result of JavaScript is still of Number type (double precision floating point number), but it will face the problem of accuracy loss when there are very large values. PHP will automatically convert to float when it is outside the integer range, which may also be inaccurate, but the behavior is expected.
<?php
echo hexdec(""); // Output 0
echo hexdec("xyz"); // Output 0
?>
console.log(parseInt("", 16)); // Output NaN
console.log(parseInt("xyz", 16)); // Output NaN
This is also a big difference between PHP and JavaScript: PHP is more tolerant, empty strings are also regarded as 0 , while JavaScript is more stringent, returning NaN .
In some scenarios where hexadecimal represents color or hash parameters, it may be necessary to parse these parameters into integers for processing, such as the following URL example:
<?php
$hex = $_GET['value'] ?? '1F';
$decimal = hexdec($hex);
echo "Decimal value: $decimal";
?>
When the URL is:
https://gitbox.net/color.php?value=FF
Similar processing methods in JavaScript are as follows:
const urlParams = new URLSearchParams(window.location.search);
const hex = urlParams.get('value') || "1F";
const decimal = parseInt(hex, 16);
console.log("Decimal value:", decimal);
characteristic | PHP hexdec | JS parseInt |
---|---|---|
Illegal character processing | Return part of the parsed value | Returns partially parsed value or NaN |
Empty string processing | Return to 0 | Return to NaN |
Accuracy and range | Supports large numbers but converts to float | Accuracy may be lost |
Case sensitivity | Insensitive | Insensitive |
Requires a specified binary | no | Yes (recommended) |
Although both hexdec and parseInt can complete the basic tasks of hex to decimal, there are differences in handling boundary input, type compatibility, and error tolerance. Understanding these differences is important for developing cross-language systems, processing user input, and debugging data.