Home  >  Article  >  Backend Development  >  How to convert string to byte array in php

How to convert string to byte array in php

WBOY
WBOYOriginal
2023-05-07 21:07:07805browse

As a high-level programming language, PHP has many powerful functions and features, among which string operation is one of the common functions. In actual development, we often need to convert strings into byte arrays to facilitate some binary data operations. This article will focus on how to convert a string into a byte array in PHP.

1. First of all, you need to clarify what a byte array is.

In computers, bytes are a basic unit of measurement, which can measure the size of binary data. The byte array, as the name suggests, is a data structure composed of multiple bytes. In PHP, byte arrays are usually operated using byte array functions, including obtaining the length of byte arrays, intercepting, merging, etc. Before performing byte array operations, the string needs to be converted into a byte array.

2. Use PHP built-in functions to convert a string into a byte array

1. Use the str_split function

The str_split function can convert a string into a character array. Because a character is actually a byte, this is equivalent to converting a string into a byte array. The syntax is as follows:

array str_split ( string $string [, int $split_length = 1 ] )

Among them, $string represents the string to be converted, $split_length represents the length of each byte array element, and the default is 1.

For example:

$str = 'Hello World';
$bytes = str_split($str);
print_r($bytes);

Output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>  
    [6] => W
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)

As you can see, we successfully converted the string into an array containing multiple bytes.

2. Use the unpack function

The unpack function is a powerful function in PHP that can parse binary data into an array according to the specified format. We can use this function to convert a string into a byte array. The syntax is as follows:

array unpack ( string $format , string $data )

Among them, $format represents the format of the data, and $data represents the binary data to be parsed.

For example:

$str = 'Hello World';
$bytes = unpack('C*', $str);
print_r($bytes);

Output:

Array
(
    [1] => 72
    [2] => 101
    [3] => 108
    [4] => 108
    [5] => 111
    [6] => 32
    [7] => 87
    [8] => 111
    [9] => 114
    [10] => 108
    [11] => 100
)

As you can see, we also successfully converted $string into a byte array.

3. Summary

This article mainly introduces how to convert a string into a byte array in PHP. We can use the str_split function or the unpack function to achieve this. No matter which method is used, you can end up with an array containing multiple bytes, which is convenient for subsequent byte array operations. In actual development, this function is relatively common. I hope this article can provide some help.

The above is the detailed content of How to convert string to byte array in php. 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