Current Location: Home> Latest Articles> Practical Tips for Using the hexdec Function to Handle UUIDs or GUIDs

Practical Tips for Using the hexdec Function to Handle UUIDs or GUIDs

gitbox 2025-06-11

What is the hexdec Function?

hexdec is a built-in PHP function that converts a hexadecimal string into a decimal number. For example:

<?php
echo hexdec('1a'); // Outputs 26
?>

However, it is important to note that hexdec can only process digits and hexadecimal characters (0-9, a-f) within the string. If the input contains non-hexadecimal characters (such as the hyphens - found in UUIDs), the string must be cleaned first.


Basic Method for Converting UUID to Number

A typical UUID looks like this:

550e8400-e29b-41d4-a716-446655440000

When using hexdec, you first remove the hyphens:

<?php
$uuid = '550e8400-e29b-41d4-a716-446655440000';
$cleanUuid = str_replace('-', '', $uuid);
echo hexdec($cleanUuid);
?>

Note: Because UUIDs are quite long (32 hexadecimal characters), their decimal values may exceed PHP’s integer range, causing hexdec to return only partially correct results.


Handling UUIDs That Exceed the Integer Range

PHP integers support up to 64-bit numbers on 64-bit systems. For 32-byte UUIDs, directly converting with hexdec often causes overflow. Here are some solutions:

  1. Use BC Math or GMP Extensions
    These extensions support calculations on arbitrarily large numbers.

<?php
$uuid = '550e8400-e29b-41d4-a716-446655440000';
$cleanUuid = str_replace('-', '', $uuid);
<p>function bcHexDec($hex) {<br>
$dec = '0';<br>
$len = strlen($hex);<br>
for ($i = 0; $i < $len; $i++) {<br>
$current = hexdec($hex[$i]);<br>
$dec = bcmul($dec, '16');<br>
$dec = bcadd($dec, $current);<br>
}<br>
return $dec;<br>
}</p>
<p>echo bcHexDec($cleanUuid);<br>
?><br>

  1. Convert Only Part of the UUID
    For example, convert just the first 8 characters of the UUID:

<?php
$uuid = '550e8400-e29b-41d4-a716-446655440000';
$part = substr(str_replace('-', '', $uuid), 0, 8);
echo hexdec($part);
?>

This approach yields a smaller number that can be used for indexing or quick comparisons.


Summary of Practical Tips

  • Clean hyphens from the UUID first to ensure the string passed to hexdec contains only hexadecimal characters.

  • Avoid using hexdec on overly long UUIDs; instead, use BCMath or GMP for large number conversion.

  • Extract and convert only parts of the UUID to reduce the numeric range.

  • Combine with database index design to use the converted numeric values for sorting and quick lookup.


Additional Tip: Using hexdec to Get UUID from URL Parameters

If your project’s URL contains a UUID parameter and you want to quickly convert it to a number for processing, you can combine PHP’s $_GET like this:

<?php
if (isset($_GET['uuid'])) {
    $uuid = $_GET['uuid'];
    $cleanUuid = str_replace('-', '', $uuid);
    echo hexdec(substr($cleanUuid, 0, 8)); // Convert only the first 8 characters
}
?>

Example access URL:

https://gitbox.net/project/page.php?uuid=550e8400-e29b-41d4-a716-446655440000