


How Can I Convert a PHP String into a Byte Array Using `unpack()`?
Converting a String to a Byte Array in PHP
In PHP, you may encounter the need to convert a string into a byte array, especially when interfacing with systems expecting binary data formats.
GetBytes() Equivalent in PHP
Similar to Java's getBytes() method, PHP offers the unpack() function to accomplish this task. Unlike getBytes(), which returns a byte array representing the string's Unicode code points, unpack() allows you to specify a custom format to extract specific data.
Unpacking a String into an Integer Array
To obtain an integer array representing the byte values of a string, use the following unpack() format:
$byte_array = unpack('C*', $string);
The format string 'C*' indicates that we want to unpack a sequence of unsigned characters (in ranges [0, 255]), representing the byte values of the string.
Example
Let's take the example string: "The quick fox jumped over the lazy brown dog". Using the above approach, we can obtain the corresponding byte array:
$string = "The quick fox jumped over the lazy brown dog"; $byte_array = unpack('C*', $string); var_dump($byte_array);
Output:
array(44) { [1] => int(84) [2] => int(104) [3] => int(101) [4] => int(32) [5] => int(113) [6] => int(117) [7] => int(105) [8] => int(99) [9] => int(107) [10] => int(32) [11] => int(102) [12] => int(111) [13] => int(120) [14] => int(32) [15] => int(106) [16] => int(117) [17] => int(109) [18] => int(112) [19] => int(101) [20] => int(100) [21] => int(32) [22] => int(111) [23] => int(118) [24] => int(101) [25] => int(114) [26] => int(32) [27] => int(116) [28] => int(104) [29] => int(101) [30] => int(32) [31] => int(108) [32] => int(97) [33] => int(122) [34] => int(121) [35] => int(32) [36] => int(98) [37] => int(114) [38] => int(111) [39] => int(119) [40] => int(110) [41] => int(32) [42] => int(100) [43] => int(111) [44] => int(103) }
As you can see, the output array contains the byte values of each character in the string, indexed from 1.
The above is the detailed content of How Can I Convert a PHP String into a Byte Array Using `unpack()`?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
