Home > Article > Backend Development > Convert php string to byte array
In PHP, converting a string into a byte array is a very useful operation. Byte arrays are especially important when working with binary data. In this article, we will take a closer look at how to convert a PHP string to a byte array.
1. What is a byte array?
Byte array, that is, an array composed of bytes, is a format used to represent data. In PHP, each character is represented by one or more bytes. Therefore, a string can be viewed as a byte array.
2. How to convert a string into a byte array?
In PHP, we can convert a string to a byte array in two ways.
1. Use the str_split() function
The str_split() function can split a string into an array according to the specified length, and each element is a string of equal length.
The following is a sample code that uses the str_split() function to convert a string into a byte array:
$str = 'Hello world!'; $bytes = str_split($str);
The $bytes array here stores the corresponding character of each character in the $str string ASCII code value. If we want to convert the elements in the $bytes array to hexadecimal format, we can use the dechex() function.
The following is the code to convert the elements in the $bytes array to hexadecimal format:
foreach ($bytes as $byte) { $hex[] = dechex(ord($byte)); }
In the above code, we use the ord() function to convert each character into Its corresponding ASCII code value, and use the dechex() function to convert it to hexadecimal format.
2. Use the unpack() function
The unpack() function can convert a binary string into an array, where each element is the decimal value of the corresponding byte in the string.
Here is the sample code to convert a string into a byte array using unpack() function:
$str = 'Hello world!'; $bytes = unpack('C*', $str);
In the above code, we have used the unpack() function and the character 'C' . This character indicates that each byte is treated as an unsigned character. Additionally, '*' means reading all binary data.
3. How to convert byte array to string?
We can use the implode() function to convert a byte array into a string. The following is a sample code that uses the implode() function to convert the $bytes array to a string:
$str = implode('', array_map('chr', $bytes));
In the above code, we used the array_map() function and chr() function. The array_map() function takes each element (ie byte value) in the $bytes array as a parameter of the chr() function and converts each byte value into ASCII characters. Finally, use the implode() function to concatenate all characters into a string.
4. How to test the performance of converting string to byte array?
Converting string to byte array is a common operation, and we need to test its performance to ensure its efficiency.
The following is a simple test, which can be used to test the performance of converting strings to byte arrays:
$testData = str_repeat('a', 1024 * 1024); $times = 100; $start = microtime(true); for ($i = 0; $i < $times; $i++) { $bytes = unpack('C*', $testData); } $end = microtime(true); echo 'Time per loop: ' . (1000 * ($end - $start) / $times) . 'ms';
In the above code, we use the str_repeat() function to generate a 1MB size string and convert it to a byte array. We then loop this operation 100 times and output the time required for each loop.
According to the test results, on my machine (MacBook Pro, 2.7GHz Intel Core i5, 8GB RAM), the average running time of this operation is 7~8 milliseconds.
The above is the detailed content of Convert php string to byte array. For more information, please follow other related articles on the PHP Chinese website!