Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary Talk Back
08-16-2009, 08:11 AM
Post: #1
Binary Talk Back
I saw a topic header here and it gave me an idea. Why not write a program that receives user input and then displays what was received in binary.

example of user input:
"text message"

would translate to:
11101000110010101111000011101000010000011011010110010101110011011100110110000101​1001110110010100001010
Find all posts by this user
Quote this message in a reply
08-16-2009, 08:40 AM
Post: #2
RE: Binary Talk Back
Ah so an ASCII to binary converter?
I'll try to this one

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
08-16-2009, 09:00 AM
Post: #3
RE: Binary Talk Back
asci tablature to file, then import when it needs? Smile But I am lazy to write program Tongue

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
08-16-2009, 09:58 PM
Post: #4
RE: Binary Talk Back
I was thinking of making ASCII to Binary and Binary to ASCII. Basically you need to make functions Bin to Dec and Dec to Bin !
I already have Decimal to Binary :
Code:
program dectobin;
uses wincrt;
var
a,k:integer;
function decimal_to_binary(n,t:longint):string;
var
  a:array[1..100] of 0..1;
  k,i:integer;
  s:string;

  function power(n,k:longint):longint;
  var
    i,p:longint;
  begin
    p:=1;
    for i:=1 to k do
    p:=p*n;
    power:=p;
  end;

function add_zeros(s:string;k:longint):string;
var
  i:longint;
begin
   for i:=1 to k-length(s) do
   s:='0'+s;
   add_zeros:=s;
end;

function inttostr(n:longint):string;
var
  i:integer;
  h:array[0..9] of string;
  b:boolean;
  s:string;
begin
h[0]:='0'; h[1]:='1'; h[2]:='2'; h[3]:='3';
h[4]:='4'; h[5]:='5'; h[6]:='6'; h[7]:='7';
h[8]:='8'; h[9]:='9';
b:=false;
s:='';
if n<0 then
begin
n:=-n;
b:=true;
end;
repeat
begin
   s:=h[n mod 10]+s;
   n:=n div 10;
end;
until n=0;
if b=true then
s:='-'+s;
inttostr:=s;
end;

begin
if n>power(2,t) then
decimal_to_binary:='0'
else
begin
k:=0;
s:='';
while n>0 do
begin
  k:=k+1;
  a[k]:=n mod 2 ;
  n:=n div 2;
end;
for i:=k downto 1 do
s:=s+inttostr(a[i]);
s:=add_zeros(s,t);
decimal_to_binary:=s;
end;
end;

begin
writeln('dec to bin');
readln(a,k);
writeln(decimal_to_binary(a,k));
end.
I made it some time ago, but it wasn't optimized, and adjusted.
So i made it faster and with some checking functions inside.
It writes binary as a string, but you have to enter decimal and class, or length of binary file in bits (8 for ASCII).
After that i was thinking to make bintodec() function. So i think i have the right algorithm. It basically powers 2 and adds on variable.
Also there is an inside function that deletes zeros in front (it is recursive).
So here is the code wit whole program :
Code:
program bintodec_program;
uses wincrt;
var
s:string;
function bintodec(s:string):longint;
var
  i,p,br:longint;
  function power(n,k:longint):longint;
  var
    i,p:longint;
  begin
    p:=1;
    for i:=1 to k-1 do
    p:=p*n;
    power:=p;
  end;
  function delete_zeros_in_front(s:string):string;
  begin
    if s[1]='1' then
      delete_zeros_in_front:=s
    else
      delete_zeros_in_front:=
      delete_zeros_in_front(copy(s,2,length(s)));
  end;
begin
  p:=0;
  s:=delete_zeros_in_front(s);
  br:=length(s);
  for i:=1 to length(s) do
  if s[i]='1' then
    begin
      p:=p+power(2,br);
      br:=br-1;
    end;
  bintodec:=p;    
end;
begin
writeln('Enter binary number !');
readln(s);
writeln(bintodec(s));
end.
And if you join these two functions together with ord() and chr() functions in Pascal, you'll getascii to bin and bin to ascii converter, like this :
Code:
program ascii_to_bin_and_reversa;
uses wincrt;
var
  int,i,br:longint;
  loc_of_inp,loc_of_out,str1,str2:string;
  input,output:text;
  choice:string;
  label top;
function bintodec(s:string):longint;
var
  i,p,br:longint;
  function power(n,k:longint):longint;
  var
    i,p:longint;
  begin
    p:=1;
    for i:=1 to k-1 do
    p:=p*n;
    power:=p;
  end;
  function delete_zeros_in_front(s:string):string;
  begin
    if s[1]='1' then
      delete_zeros_in_front:=s
    else
      delete_zeros_in_front:=
      delete_zeros_in_front(copy(s,2,length(s)));
  end;
begin
  p:=0;
  s:=delete_zeros_in_front(s);
  br:=length(s);
  for i:=1 to length(s) do
  if s[i]='1' then
    begin
      p:=p+power(2,br);
      br:=br-1;
    end;
  bintodec:=p;    
end;
function dectobin(n,t:longint):string;
var
  a:array[1..100] of 0..1;
  k,i:integer;
  s:string;

  function power(n,k:longint):longint;
  var
    i,p:longint;
  begin
    p:=1;
    for i:=1 to k do
    p:=p*n;
    power:=p;
  end;
function add_zeros(s:string;k:longint):string;
var
  i:longint;
begin
   for i:=1 to k-length(s) do
   s:='0'+s;
   add_zeros:=s;
end;
function inttostr(n:longint):string;
var
  i:integer;
  h:array[0..9] of string;
  b:boolean;
  s:string;
begin
h[0]:='0'; h[1]:='1'; h[2]:='2'; h[3]:='3';
h[4]:='4'; h[5]:='5'; h[6]:='6'; h[7]:='7';
h[8]:='8'; h[9]:='9';
b:=false;
s:='';
if n<0 then
begin
n:=-n;
b:=true;
end;
repeat
begin
   s:=h[n mod 10]+s;
   n:=n div 10;
end;
until n=0;
if b=true then
s:='-'+s;
inttostr:=s;
end;
begin
if n>power(2,t) then
dectobin:='0'
else
begin
k:=0;
s:='';
while n>0 do
begin
  k:=k+1;
  a[k]:=n mod 2 ;
  n:=n div 2;
end;
for i:=k downto 1 do
s:=s+inttostr(a[i]);
s:=add_zeros(s,t);
dectobin:=s;
end;
end;
begin
top:
writeln('Binary to Decimal Converter as');
writeln('  well as Decimal to Binary');
choice:='';
repeat
   begin
    writeln('Choose :');
    writeln('1 - ASCII to Bin');
    writeln('2 - Bin to ASCII');
    readln(choice);
    end;
   until (choice='1') or (choice='2');
if choice='1' then
  begin
    writeln('Enter location of input ?');
    readln(loc_of_inp);
    writeln('Enter location of output ?');
    readln(loc_of_out);
    assign(input,loc_of_inp); reset(input);
    assign(output,loc_of_out); rewrite(output);
    repeat
      begin
        str1:='';
        str2:='';
         readln(input,str1);
         for i:=1 to length(str1) do
          str2:=str2+dectobin(ord(str1[i]),8);
          writeln(output,str2);
      end;
    until eof(input);
     close(input);
     close(output);
     writeln('Process completed !');
  end
else
begin
    writeln('Enter location of input ?');
    readln(loc_of_inp);
    writeln('Enter location of output ?');
    readln(loc_of_out);
    assign(input,loc_of_inp); reset(input);
    assign(output,loc_of_out); rewrite(output);
    repeat
      begin
        str1:='';
        str2:='';
        readln(input,str1);
         for i:=1 to length(str1) do
          if ((i-1) mod 8 = 0) or (i=1)  then
            begin
              str2:=str2+chr(bintodec(copy(str1,i,8)));
            end;
            writeln(output,str2);
      end;
    until eof(input);
    close(input);
    close(output);
    writeln('Process completed !');
end;
writeln('Press Enter to try again !');
readln;
clrscr;
goto top;    
end.
It is with files, and you need to have those files created before using them !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
08-17-2009, 12:49 AM
Post: #5
RE: Binary Talk Back
This is my solution:

Code:
#include <stdio.h>
#include <stdlib.h>

#define ONE 0x80

int main(int argc, char**argv)
{
    char ch;
    int i = 0;
    while (ch != EOF)
    {
        fputs("\nenter a string of stuff(EOF to exit)->", stdout);
        while ((ch = fgetc(stdin)) != '\n')
        {
            if (ch == EOF) break;

            for (i= 0; i < 8; ++i)
            {
                if (ch & ONE)
                {
                    fputs("1", stdout);
                }
                else
                {
                    fputs("0", stdout);
                }
                ch = ch<<1;
            }
        }
    }
    fputs("\n", stdout);
    exit(EXIT_SUCCESS);
}
Find all posts by this user
Quote this message in a reply
08-17-2009, 04:38 AM
Post: #6
RE: Binary Talk Back
Great G4143, as you can see, solution is much longer in Pascal, because i made everything from scratch, and for both dec to bin and bin to dec,
but i think i understand this, except one line "#define ONE 0x80", and everything about that ONE, can you explain ?

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
08-17-2009, 05:19 AM
Post: #7
RE: Binary Talk Back
(08-17-2009 04:38 AM)drdebcol Wrote:  Great G4143, as you can see, solution is much longer in Pascal, because i made everything from scratch, and for both dec to bin and bin to dec,
but i think i understand this, except one line "#define ONE 0x80", and everything about that ONE, can you explain ?

I use 0x80(#define ONE 0x80) to mask out the first bit of the byte. If you look at the binary value of 0x80 it works out to be: 10000000 so any byte that is "and" with 0x80 will return zero if the first bit is zero and not zero if the first bit is one...G143
Find all posts by this user
Quote this message in a reply
08-17-2009, 05:22 AM
Post: #8
RE: Binary Talk Back
(08-17-2009 05:19 AM)G4143 Wrote:  
(08-17-2009 04:38 AM)drdebcol Wrote:  Great G4143, as you can see, solution is much longer in Pascal, because i made everything from scratch, and for both dec to bin and bin to dec,
but i think i understand this, except one line "#define ONE 0x80", and everything about that ONE, can you explain ?

I use 0x80(#define ONE 0x80) to mask out the first bit of the byte. If you look at the binary value of 0x80 it works out to be: 10000000 so any byte that is "and" with 0x80 will return zero if the first bit is zero and not zero if the first bit is one...G143
OK now i understand, but Pascal is different, i think you can not define those values !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
08-17-2009, 06:59 AM (This post was last modified: 08-17-2009 07:01 AM by G4143.)
Post: #9
RE: Binary Talk Back
(08-17-2009 05:22 AM)drdebcol Wrote:  
(08-17-2009 05:19 AM)G4143 Wrote:  
(08-17-2009 04:38 AM)drdebcol Wrote:  Great G4143, as you can see, solution is much longer in Pascal, because i made everything from scratch, and for both dec to bin and bin to dec,
but i think i understand this, except one line "#define ONE 0x80", and everything about that ONE, can you explain ?

I use 0x80(#define ONE 0x80) to mask out the first bit of the byte. If you look at the binary value of 0x80 it works out to be: 10000000 so any byte that is "and" with 0x80 will return zero if the first bit is zero and not zero if the first bit is one...G143
OK now i understand, but Pascal is different, i think you can not define those values !

In C the "#define ONE 0x80" statement is a macro that gets processed with the pre-processor...essentially the pre-processor scans the source code for any instance of ONE and replaces the it with 0x80 so that the compiler never sees ONE only 0x80 hex...G4143
Find all posts by this user
Quote this message in a reply
08-17-2009, 08:38 PM (This post was last modified: 08-17-2009 08:38 PM by l3g1sl4tor.)
Post: #10
RE: Binary Talk Back
My weak knowledge in C is ending here Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: