Page 1 of 2
Binary[edited]
Posted: Tue Jun 15, 2004 3:14 pm
by dull1554
can i ask you guys this, i did a little searching in the manual but did not wonder upon anything useful.....
is there a predefined function for converting ASCII text to BINARY and visa versa?
[edit]
let me re-phrase the question....
if you have text like
blah i want to beable to convert it to the binary equivilant
like 01101011001011001010011110011101
[/edit]
thanx
Posted: Tue Jun 15, 2004 3:16 pm
by feyd
ascii text is already binary..
Posted: Tue Jun 15, 2004 3:29 pm
by feyd
untested
Code: Select all
<?php
function convertToVisualBinary($string)
{
$ret = '';
for($x = 0, $y = strlen($string); $x < $y; $x++)
{
for($z = 0; $z < 8; $z++)
{
$ret .= (($string{$x} & (1 << (7-$z)))?'1':'0');
}
}
return $ret;
}
?>
Posted: Tue Jun 15, 2004 3:31 pm
by d3ad1ysp0rk
feyd, why not replace
Code: Select all
for($x = 0, $y = strlen($string); $x < $y; $x++)
with
Code: Select all
for($x = 0;$x < strlen($string); $x++)
or is there something different between those two things?
Posted: Tue Jun 15, 2004 3:32 pm
by dull1554
for the word lol it returns 000000000000000000000000
Posted: Tue Jun 15, 2004 3:33 pm
by dull1554
id try to fix it but i do not understand what that is supposed to do
Posted: Tue Jun 15, 2004 3:35 pm
by feyd
LiLpunkSkateR wrote:feyd, why not replace
Code: Select all
for($x = 0, $y = strlen($string); $x < $y; $x++)
with
Code: Select all
for($x = 0;$x < strlen($string); $x++)
or is there something different between those two things?
calling strlen() all the time for long strings will slow the script..
Posted: Tue Jun 15, 2004 3:38 pm
by feyd
Code: Select all
<?php
function convertToVisualBinary($string)
{
$ret = '';
for($x = 0, $y = strlen($string); $x < $y; $x++)
{
for($z = 0; $z < 8; $z++)
{
$ret .= ((ord($string{$x}) & (1 << (7-$z)))?'1':'0');
}
}
return $ret;
}
echo convertToVisualBinary('lol');
?>
Posted: Tue Jun 15, 2004 3:38 pm
by d3ad1ysp0rk
hmm.. forgot it called the second parameters each time.. whoops.

Posted: Tue Jun 15, 2004 3:44 pm
by dull1554
feyd is there any way you could explain to me how that works
it does work perfect but i would also like to be able to go in reverse, but i would like to do that, but i think ti would be great if you could show me what
Code: Select all
((ord($string{$x}) & (1 << (7-$z)))?'1':'0');
does
Posted: Tue Jun 15, 2004 3:50 pm
by d3ad1ysp0rk
ord converts the first character of string to ascii.. not sure about the second part tho.
Posted: Tue Jun 15, 2004 3:50 pm
by feyd
ord() returns the numeric value of a character.
$string{$x} returns the x'th character in the string.
<< is a bitshift operator... $x << $y, shifts $x $y bits left.
?: is the ternary operator.. a compact if statement basically.. expression ? true statement : false statement ..... is the syntax..
Posted: Tue Jun 15, 2004 4:00 pm
by redmonkey
ASCII to binary and back again....
Code: Select all
<?php
function asc2bin($in)
{
$out = '';
for ($i = 0, $len = strlen($in); $i < $len; $i++)
{
$out .= sprintf("%08b",ord($in{$i}));
}
return $out;
}
function bin2asc($in)
{
$out = '';
for ($i = 0, $len = strlen($in); $i < $len; $i +=
{
$out .= chr(bindec(substr($in,$i,8)));
}
return $out;
}
?>
Posted: Tue Jun 15, 2004 4:08 pm
by feyd
heh, forgot about the srintf param...
Posted: Tue Jun 15, 2004 4:30 pm
by dull1554
Code: Select all
$var = "a";
echo asc2bin($var);
echo bin2asc($var);
returns this
Code: Select all
01100001
ї]//this being a box like a unknown character
anythoghts