Home  >  Article  >  Backend Development  >  php convert string to hexadecimal

php convert string to hexadecimal

WBOY
WBOYOriginal
2023-05-24 17:59:082331browse

In the field of web development, it is often necessary to encode and decode data. Among them, converting strings to hexadecimal is a common requirement. For example, when encrypting, transmitting, or storing data, the data needs to be converted to hexadecimal format. In PHP, it is also very convenient to convert into functions. Let’s introduce the specific implementation method.

1. Use the bin2hex() function

PHP provides the bin2hex() function, through which binary data can be converted into a hexadecimal string. Since strings are essentially binary data, we can first convert the string that needs to be converted into binary format, and then use the bin2hex() function to convert the string into hexadecimal. The specific code is as follows:

$str = 'hello world';
$hex_str = bin2hex($str); 
echo $hex_str;

In the above code, the string "hello world" is converted into a binary string, and then converted into a hexadecimal string through the bin2hex() function. The final output result is:

68656c6c6f20776f726c64

This result looks strange, but it is actually the hexadecimal representation of "hello world". Every two characters correspond to one byte, making it easy for further processing and transmission.

2. Custom function to convert string to hexadecimal

In addition to using built-in functions, we can also use custom functions to convert strings to hexadecimal. A simple implementation code is provided below:

function str2hex($str) {
    $hex='';
    for ($i=0; $i<strlen($str); $i++) {
        $ord=ord($str[$i]);
        $hexCode=dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return $hex;
}

$str = 'hello world';
$hex_str = str2hex($str);
echo $hex_str;

In the above code, we first define a custom function str2hex(), which can convert a string to hexadecimal format. The specific implementation principle is as follows:

  1. Traverse the string and use the ord() function to convert each character into ASCII code.
  2. Convert ASCII code to hexadecimal format using the dechex() function.
  3. When the currently obtained hexadecimal digit is less than two digits, add zeros in front.
  4. Splice the hexadecimal format of each character together to finally get the hexadecimal format of the entire string.

The output result is the same as the result of using the built-in function bin2hex(), both are:

68656c6c6f20776f726c64

Summary

In PHP, convert the string to hexadecimal There are two main ways to implement hex format: using the built-in function bin2hex() or a custom function. Among them, it is easier to use built-in functions, but custom functions have higher flexibility and can perform more customized calculations and conversions. Developers can choose a suitable method to implement based on their actual needs.

The above is the detailed content of php convert string to hexadecimal. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn