Is there a way to convert a string to binary then back again in the standard PHP library?
To clarify what I'm trying to do is store a password on a database. I'm going to convert it first using a hash function then eventually store it as binary.
I've found the best way is to use this function. Seems to hash and output in binary at the same time.
http://php.net/manual/en/function.hash-hmac.php
You want to use pack
and base_convert
.
// Convert a string into binary
// Should output: 0101001101110100011000010110001101101011
$value = unpack('H*', "Stack");
echo base_convert($value[1], 16, 2);
// Convert binary into a string
// Should output: Stack
echo pack('H*', base_convert('0101001101110100011000010110001101101011', 2, 16));
Yes, sure!
There...
$bin = decbin(ord($char));
... and back again.
$char = chr(bindec($bin));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With