allex01 Posted August 11, 2008 Share Posted August 11, 2008 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), ); $_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 More sharing options...
lemmin Posted August 11, 2008 Share Posted August 11, 2008 Do you mean like this? $text = "text"; for ($i=0;$i<strlen($text);$i++) echo decbin(ord($text[$i])) . "<br>"; Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614012 Share on other sites More sharing options...
allex01 Posted August 11, 2008 Author Share Posted August 11, 2008 Yes exactly. How do we do it backwords from binary to text. I have this bnary code 01110100 01100101 01110011 01110100 01101001 01101110 01100111 that should read "testing". Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614021 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 You probably want the opposite <?php $str = '01110100 01100101 01110011 01110100 01101001 01101110 01100111'; $arr = explode( ' ', $str ); foreach ( $arr as $chr ) { echo chr( bindec($chr) ); } ?> Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614045 Share on other sites More sharing options...
allex01 Posted August 11, 2008 Author Share Posted August 11, 2008 Perfect. I appriciate the help Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614056 Share on other sites More sharing options...
allex01 Posted August 12, 2008 Author Share Posted August 12, 2008 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614138 Share on other sites More sharing options...
Barand Posted August 12, 2008 Share Posted August 12, 2008 <?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'; ?> Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614141 Share on other sites More sharing options...
allex01 Posted August 12, 2008 Author Share Posted August 12, 2008 Works great. Thanks for everyones help. Link to comment https://forums.phpfreaks.com/topic/119211-solved-text-to-binary/#findComment-614198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.