j2k
May 30th, 2002, 02:57 PM
Hi all,
How can you convert an ascii string (such as "Hello World") to binary 0 and 1s in PHP?
Thanks in advance.
scoutt
May 31st, 2002, 08:09 AM
in php you would have to make a function yourself and convert all letters to hex then go to binary. that would be pretty hard to come up with.
Martin Wilson
May 31st, 2002, 10:14 AM
This seems to work but I am sure there is an easier method:<?
$DecString = "Hello World";
for ($i = 0; $i <= strlen($DecString); $i++) {
$char = substr($DecString,$i,1);
$dec = ord($char);
$bin = base_convert($dec, 10, 2);
$bin = str_pad($bin, 8, "0", STR_PAD_LEFT);
print $bin;
}
?>