Home  >  Article  >  Backend Development  >  How to convert string to bytes in PHP and process it

How to convert string to bytes in PHP and process it

PHPz
PHPzOriginal
2023-04-04 09:13:30908browse

PHP is a very popular back-end programming language. One of its advantages is its high flexibility and the ability to handle a variety of data types, including strings and bytes. This article mainly introduces how to convert strings into bytes and process them.

1. Understand strings and bytes

A string is a text sequence containing zero or more characters. In PHP, strings can be represented using quotes or single quotes. For example:

$str = "Hello world!";

When text data is stored in a computer, a sequence of binary numbers, that is, bytes, is used. Each character corresponds to one or more bytes. In PHP, you can use the ord() function to obtain the ASCII code value of a character, and use the chr() function to convert the ASCII value into a character. For example:

$ch = "A";
$ascii = ord($ch); // 返回65
$ch = chr($ascii); // 返回"A"

2. Convert string to byte array

In order to convert string to bytes, you can use PHP's unpack() function. This function will unpack a binary string into an array. Each unpacked element is one byte.

$str = "Hello World!";
$bytes = unpack("C*", $str);
print_r($bytes); // array(1, 2, 3, 3, 4, ...)

Here, the unpack() function converts the string "Hello World!" into a byte array and stores it in the $bytes array. C *The parameter tells the function to unpack each character in the string into a byte, which means that each element in the array is an integer value.

3. Processing byte arrays

Once the string is converted to bytes, it can be processed. For example, you can use a loop to iterate through each byte in a byte array and operate on it. In the example below, the following code will check each byte in the byte array and if they are below 128, print the ASCII character for that byte, otherwise print an error message.

$str = "Hello World!";
$bytes = unpack("C*", $str);
foreach ($bytes as $b) {
    if ($b < 128) {
        echo chr($b);
    } else {
        echo "Invalid byte: " . $b;
    }
}

In this example, we use foreach() to loop through each byte in the byte array $bytes and check if its value is less than 128. If it is, then use the chr() function to print the ASCII character of that byte. Otherwise, an error message will be printed indicating that it is an invalid byte.

4. Convert bytes to string

In some cases, you may need to convert the byte array back to a string. You can do this using the pack() function. The pack() function packs the given data into a binary string. You can convert a byte array into a string using the implode() function and pass this string as a parameter to the pack() function.

$bytes = array(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33);
$str = implode(array_map("chr", $bytes));
$binStr = pack("A*", $str);
echo $binStr; // 输出"Hello World!"

Here, we define a byte array $bytes, which contains the ASCII value of each character of the string "Hello World!". We use the array_map() function to apply the chr() function to each element in the byte array. We then use the implode() function to convert the byte array to a string, and pass the string as a parameter to the pack() function. A *The parameter means wrapping the string until the end of the string.

5. Conclusion

Converting strings to bytes is a common task in PHP. You can use the unpack() function to convert a string to a byte array and the pack() function to convert a byte array back to a string. Using these functions, you can perform various operations on byte arrays, such as performing encryption and hash calculations. By mastering these techniques, you can easily handle byte and string data.

The above is the detailed content of How to convert string to bytes in PHP and process it. 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