Jump to content

[SOLVED] text to binary


allex01

Recommended Posts

I'm using the below function to output binary from text. I can't get it to work. What am i doing wrong. Is there an easier way of doing this.

 

<?php

function bin2text($bin_str)

{

    $text_str = '';

    $chars = explode("\n", chunk_split(str_replace("\n", '', $bin_str), 8));

    $_I = count($chars);

    for($i = 0; $i < $_I; $text_str .= chr(bindec($chars[$i])), $i  );

    return $text_str;

}

 

$text="text";

bin2text($text);

?>

Link to comment
https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/
Share on other sites

I have the following binary code which represents the word "localhost"

01101100 01101111 01100011 01100001 01101100 01101000 01101111 01110011 01110100

 

I'm trying to write a small script that would compare the binary code text output to the domain name $_SERVER['HTTP_HOST']; I'm testing this locally on localhost.

The binary numbers when converted to text using the function below outputs localhost. When i compare with $_SERVER['HTTP_HOST']; which should also output localhost i cannot get the output yes. What am i doing wrong.

 

<?php

function bin($str)     {

$arr = explode( ' ', $str );

foreach ( $arr as $chr ) {

echo chr( bindec($chr) );

}}

 

$site= $_SERVER['HTTP_HOST'];

$bin = bin("01101100 01101111 01100011 01100001 01101100 01101000 01101111 01110011 01110100");

echo "</br>" .$site;

 

if($site == $bin) {

echo "yes";

}

 

?>

 

<?php
function bin2str($bin) {
    $b_arr = explode(' ', $bin);
    $res = '';
    foreach ($b_arr as $b)
        $res .= chr(bindec($b));
    return $res;
}

$binary = '01101100 01101111 01100011 01100001 01101100 01101000 01101111 01110011 01110100';
$str = bin2str($binary);

echo $str, '<br/>';
echo $_SERVER['HTTP_HOST'], '<br/>';

if ($str==$_SERVER['HTTP_HOST'])
    echo 'yes';
else 
    echo 'no';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.