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

How to Convert Text to Binary and Back Using PHP?

DDD
DDDOriginal
2024-11-04 02:59:30395browse

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>

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>

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!

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