Binary[edited]
Moderator: General Moderators
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
Binary[edited]
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
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
Last edited by dull1554 on Tue Jun 15, 2004 3:23 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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;
}
?>-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
feyd, why not replace
with
or is there something different between those two things?
Code: Select all
for($x = 0, $y = strlen($string); $x < $y; $x++)Code: Select all
for($x = 0;$x < strlen($string); $x++)- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
calling strlen() all the time for long strings will slow the script..LiLpunkSkateR wrote:feyd, why not replacewithCode: Select all
for($x = 0, $y = strlen($string); $x < $y; $x++)or is there something different between those two things?Code: Select all
for($x = 0;$x < strlen($string); $x++)
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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');
?>-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
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
does
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');-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
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;
}
?>- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
Code: Select all
$var = "a";
echo asc2bin($var);
echo bin2asc($var);Code: Select all
01100001
ї]//this being a box like a unknown character