Home > Backend Development > PHP Tutorial > How to Convert Text to Binary and Back Using PHP?

How to Convert Text to Binary and Back Using PHP?

DDD
Release: 2024-11-04 02:59:30
Original
449 people have browsed it

How to Convert Text to Binary and Back Using PHP?

Convert Text to Binary and Back Using PHP

Storing passwords securely is paramount, and utilizing binary encryption is a valuable tool. This guide demonstrates how to convert strings to binary and back in PHP.

Conversion with pack() and base_convert()

For this conversion task, PHP offers two functions: pack() and base_convert().

To convert a string to binary, use pack('H*', $string). For instance:

<code class="php">$value = unpack('H*', "Stack");
echo base_convert($value[1], 16, 2); // Outputs: 0101001101110100011000010110001101101011</code>
Copy after login

To convert binary to a string, use pack('H*', $binary). For example:

<code class="php">echo pack('H*', base_convert('0101001101110100011000010110001101101011', 2, 16)); // Outputs: Stack</code>
Copy after login

The above is the detailed content of How to Convert Text to Binary and Back Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template