Binary[edited]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Binary[edited]

Post 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
Last edited by dull1554 on Tue Jun 15, 2004 3:23 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ascii text is already binary..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
}

?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

for the word lol it returns 000000000000000000000000
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

id try to fix it but i do not understand what that is supposed to do
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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');

?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

hmm.. forgot it called the second parameters each time.. whoops. :)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

ord converts the first character of string to ascii.. not sure about the second part tho.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

heh, forgot about the srintf param...
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

Code: Select all

$var = "a";
echo asc2bin($var);
echo bin2asc($var);
returns this

Code: Select all

01100001
&#1111;]//this being a box like a unknown character
anythoghts
Post Reply