"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Convert Strings to Binary and Back in PHP?

How to Convert Strings to Binary and Back in PHP?

Published on 2024-11-08
Browse:785

How to Convert Strings to Binary and Back in PHP?

Converting Binary and Strings in PHP: A Comprehensive Guide

When working with data in PHP, it's often necessary to convert between different formats. One common conversion is between strings and binary. Whether it's for storing data securely or optimizing performance, understanding how to convert between these formats is essential.

Problem:

How to convert a string to binary, and then convert it back to a string in the standard PHP library? This is a common need for secure data storage and manipulation.

Solution:

Unlike some programming languages, PHP does not have a built-in function to directly convert a string to binary. However, a combination of the pack() and base_convert() functions can achieve this functionality.

Binary to String:

To convert a binary string back to its original string, we can use pack() and base_convert(). The pack() function takes a format string and a string of binary data and combines them to create a string. In our case, the format string is 'H*', which indicates a hexadecimal string. The base_convert() function converts a string from one base to another. In this case, we convert the hexadecimal string to the ASCII character set:

// Convert binary into a string
$string = pack('H*', base_convert('0101001101110100011000010110001101101011', 2, 16));

String to Binary:

To convert a string to binary, we can use unpack() and base_convert(). The unpack() function takes a format string and a string and extracts data from the string according to the format specified. In our case, we specify the format 'H*' to extract hexadecimal data. The base_convert() function converts a string from one base to another. Here, we convert the ASCII string to a hexadecimal string, which represents the binary representation:

// Convert a string into binary
$binary = unpack('H*', 'Stack');
echo base_convert($binary[1], 16, 2);

Example:

Let's put it all together with an example:

// Convert "Stack" to binary
$binary = unpack('H*', 'Stack');
$binaryString = base_convert($binary[1], 16, 2);
echo "Binary: $binaryString\n";

// Convert binary back to "Stack"
$asciiString = pack('H*', base_convert($binaryString, 2, 16));
echo "String: $asciiString\n";

Output:

Binary: 0101001101110100011000010110001101101011
String: Stack

By utilizing these functions, we can effectively convert between strings and binary in PHP. This knowledge is invaluable for data encryption, file manipulation, and various other tasks.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3