Home Backend Development PHP Problem How to convert string to byte array in php

How to convert string to byte array in php

Apr 20, 2023 pm 01:53 PM

In PHP, we can use several methods to convert a string to a byte array. In this article, we will focus on the two most common methods.

Method 1: Use str_split function

str_split(string$string, int$split_length = 1): Split the string into substrings, put them in an array, and return the array. The length of each string will be the length specified by the split_length parameter. If split_length is not specified, it defaults to 1.

The following is a sample code to convert a string to a byte array:

$str = "Hello World!";
$bytes = str_split($str);

foreach($bytes as $byte) {
    echo ord($byte) . " ";
}
Copy after login

In this sample code, we have used the ord function to convert each character to ASCII code, and used foreach Loop prints a byte array to the console. The output will be:

72 101 108 108 111 32 87 111 114 108 100 33
Copy after login
Copy after login

Method Two: Using the unpack function

This method requires using the pack function to pack the string into binary data. In addition, when using the unpack function, you need to specify the packed data type and convert the return value to a byte array.

The following is the sample code:

$str = "Hello World!";
$bytes = unpack('C*', pack('H*', bin2hex($str)));

foreach($bytes as $byte) {
    echo $byte . " ";
}
Copy after login

In this sample code, we have used bin2hex to convert the string into a hexadecimal string, and then used pack to pack it into binary data. When using unpack, we unpack the data using C type shared symbols and print the byte array using a foreach loop.

The output will be:

72 101 108 108 111 32 87 111 114 108 100 33
Copy after login
Copy after login

To summarize, these are two ways to convert a string to a byte array in PHP. Understanding how these methods work is critical for developers who need to perform various encryption/decryption operations.

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24