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!