Current Location: Home> Latest Articles> Why is the length of the converted string twice as long as the input?

Why is the length of the converted string twice as long as the input?

gitbox 2025-05-26

During PHP development, bin2hex() is a commonly used built-in function, mainly used to convert binary data into its hexadecimal representation. Many developers will find that the returned string length is twice the length of the original binary data after using bin2hex() . Although this phenomenon is in line with expectations, it is still worth our in-depth discussion of the reasons behind it and its potential impact on performance.

1. The function and use of bin2hex

bin2hex(string $string): string
This function takes a string parameter, converting its contents into a hexadecimal string byte. Its typical uses include:

  • Convert binary data (such as encryption results) to printable form;

  • Coding for URL security;

  • Displays raw data in debug or log.

For example:

 $input = "AB";
$output = bin2hex($input);
echo $output; // Output 4142

2. Why is the length twice the original after conversion?

This is because:

  1. The relationship between byte and hexadecimal representation : a byte is 8 bits, which can represent a value of 0~255. Each bit in hexadecimal represents 4 bits (binary), so a two-digit hexadecimal number is required to represent a byte. For example:

    • The hexadecimal value of 65 (character 'A') is 41

    • The hexadecimal value of 66 (character 'B') is 42

  2. So, each character in the input string (assuming a single-byte ASCII) is converted into a hexadecimal representation of two character lengths. Therefore, the length after conversion is naturally twice that of the original string.

This characteristic is stable and not caused by some special circumstances.

3. Performance and storage impact

When processing large data or systems requiring high performance, the increase in length caused by using bin2hex() may have a significant impact on performance and resource usage:

1. Network transmission

If you transfer data through URL parameters or API, double the data volume after encoding with bin2hex() , it may affect:

  • Bandwidth usage (especially on narrowband links such as mobile networks)

  • URL length limit (browser and server generally have constraints on URL length)

For example:

 $data = random_bytes(32); // Original32byte
$hex = bin2hex($data); // After conversion, it becomes64byte(String length)
$url = "https://gitbox.net/process.php?data=" . $hex;

If you encode a large amount of data into the URL like this, the URL length limit will be quickly reached.

2. Database storage

When storing the encoded results into the database, the storage space needs to be estimated in advance. For example, when saving the CHAR(64) field in MySQL, you should ensure that the original data does not exceed 32 bytes.

 // Must be sure before inserting hex The length will not exceed the field limit
$hex = bin2hex($binaryData);
$pdo->prepare("INSERT INTO tokens (token) VALUES (?)")->execute([$hex]);

3. Memory usage and performance

In big data processing, doubled string length means:

  • More memory allocation;

  • Slower processing speed (such as sorting, searching and other string operations);

  • May throw PHP memory limit errors (such as when handling GB-level binary log files).

4. When should we avoid using bin2hex?

Although bin2hex() is a simple hex encoding method, it is more suitable to use base64_encode() in some cases:

  • When sensitive to length : Base64 saves space than bin2hex. Although Base64 has been encoded and increased by about 33% compared to the original text, it is much compact than 100% growth bin2hex.

  • When used for URL secure transmission : combining base64_encode() and strtr() can create a URL secure base64 format, which is shorter than hexadecimal.

Example comparison:

 $data = random_bytes(10);

$hex = bin2hex($data);           // 20character
$base64 = base64_encode($data);  // about14character

// URLSafetybase64
$urlsafe = rtrim(strtr($base64, '+/', '-_'), '=');

Conclusion

The "double length" of bin2hex() is essentially derived from the basic principles of hexadecimal encoding, not the unique implementation of PHP. Although simple and readable, developers should evaluate the storage and transmission costs it brings in high-performance scenarios or applications that are sensitive to data length. Correct choice of coding method is an important part of system optimization.