Organizational Research By

Surprising Reserch Topic

convert string to binary then back again using php

convert string to binary then back again using php  using -'php,binary'

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.

asked Sep 30, 2015 by rolvyrf
0 votes

Related Hot Questions

6 Answers

0 votes

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));
answered Sep 30, 2015 by shikhar jain
0 votes

A string is just a sequence of bytes, hence it's actually binary data in PHP. What exactly are you trying to do?


If you want to store binary data in your database, the problem most often is the column definition in your database. PHP does not differentiate between binary data and strings, but databases do. In MySQL for example you should store binary data in BINARY, VARBINARY or BLOB columns.

Another option would be to base64_encode your PHP string and store it in some VARCHAR or TEXT column in the database. But be aware that the string's length will increase when base64_encode is used.

answered Sep 30, 2015 by sachin valanju
0 votes

Your hash is already binary and ready to be used with your database.

However you must need to convert it into a format the database column definition expects.

Any string in PHP (until 5.3) is a binary string. That means it contains only binary data.

However because of backwards compatiblity with PHP 6 you can already cast your string explicitly as binary:

 $string = 'my binary string';
 $binary = b'my binary string';

But that is merely for compatibility reasons, in your code you can just do:

  $string = $binary; // "convert" binary string into string
  $binary = $string  // "convert" string into binary string

Because it's the same. The "convert" is superfluous.

answered Sep 30, 2015 by nikhilapatil
0 votes

easiest way I found was to convert to HEX instead of a string. If it works for you:

$hex = bin2hex($bin); // It will convert a binary data to its hex representation

$bin = pack("H*" , $hex); // It will convert a hex to binary


$bin = hex2bin($hex); // Available only on PHP 5.4
answered Sep 30, 2015 by shegokar.anjeet
0 votes

i was looking for some string bits conversion and got here, If the next case is for you take //it so... if you want to use the bits from a string into different bits maybe this example would help

$string="1001"; //this would be 2^0*1+....0...+2^3*1=1+8=9
answered Sep 30, 2015 by suyesh.lokhande
0 votes

I would most definitely recommend using the built in standard password libraries that come with PHP - Here is a good example on how to use them.

For those coming here to figure out how to go from Binary Strings to Decimals and back, there are some good examples below.

For converting binary "strings" to decimals/chars you can do something like this...

echo bindec("00000001") . "\n";
echo bindec("00000010") . "\n";
echo bindec("00000100") . "\n";
echo bindec("00001000") . "\n";
echo bindec("00010000") . "\n";
echo bindec("00100000") . "\n";
echo bindec("01000000") . "\n";
echo bindec("10000000") . "\n";
echo bindec("01000001") . "\n";

# big binary string
echo bindec("111010110111011110000110001")."\n";

The above outputs:


For converting decimals to char/strings you can do this:

# convert to binary strings "00000001"
echo decbin(1) . "\n";
echo decbin(2) . "\n";
echo decbin(4) . "\n";
echo decbin(8) . "\n";
echo decbin(16) . "\n";
echo decbin(32) . "\n";
echo decbin(64) . "\n";
echo decbin(128) . "\n";

# convert a ascii character
echo str_pad(decbin(65), 8, 0, STR_PAD_LEFT) ."\n";

# convert a 'char'
echo str_pad(decbin(ord('A')), 8, 0, STR_PAD_LEFT) ."\n";

# big number...
echo str_pad(decbin(65535), 8, 0, STR_PAD_LEFT) ."\n";
echo str_pad(decbin(123452465), 8, 0, STR_PAD_LEFT) ."\n";

The above outputs:

answered Sep 30, 2015 by mcasudhir