Solved

binary

Posted on 1997-12-06
14
435 Views
Last Modified: 2010-04-16
I would like the source for a program that would take the binary (yes, the 1's and 0's) from a file and save it as a text file then also take a text file of 1's and 0's and make it back into a file. I hope I made it clear what I'm looking for if its not just ask.
0
Comment
Question by:potsy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 

Expert Comment

by:brendanmc
ID: 1216952
it sounds like you would need to understand the specific format of the file.
Different binaries have different format
0
 
LVL 1

Author Comment

by:potsy
ID: 1216953
have you ever seen a program called BEAV (Binary Editor And Viewer) it is the type program I'm want the source for I don't know how to explain it so I will post a copy of the documentation at http://www.lakenet.com/~potter/doc.zip the only thing that this program doesn't have that I would like is the ability to save the 1's and 0's as a text file.


0
 
LVL 2

Accepted Solution

by:
mitchell042997 earned 150 total points
ID: 1216954
This wouldn't be too difficult.  If you are developing this in DOS, DOS saves files in byte-size chunks (grin).  A byte is 8 bits.  Are you familiar with blockread and blockwrite?  If not, look into those.  What you will do, basically, is read in a byte from the file, convert it into ones and zeros, since you'll know the char value (0-255), this is not too difficult.  If you need an algorithm which will convert a number into a string of 8 1's and 0's, just let me know.  Then, write that string to a text file, and continue until you reach the end of the binary file.

To reverse the process, just read in eight characters from your text file, and convert that string into a number, then write it to a new file...  Pretty simple, I can provide source code if needed....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:potsy
ID: 1216955
I would like source code because I'm trying to learn how to do this and I lern best by example
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216956
OK, this is untested code, so it may be slightly erroneous, but the principles are here.  First, we must have a way to convert a 8 bit character into a string of eight 1s and 0s.

This is simple enough:

var mybyte:byte;
    bitstring:string;
    i:byte;
begin
  mybyte:=104;
  bitstring:='';

  for i:=0 to 7 do
    if (mybyte and (1 shl i) = (1 shl i)) then
      bitstring[8-i]:="1" else bitstring[8-1]:="0";
end;

For example, if mybyte = 104, bitstring would equal:

   01101000

Now, what we need to do is step through the binary file we want to convert, and write it to a text file, the converted bitstring, that is.

var textfile:text;
    binfile:file of byte;
    dummy:word;
    readbyte:byte;
begin
  assign(textfile,'somefile.txt');
  assign(binfile,'whatever.exe');

  rewrite(textfile);
  reset(binfile);

  while (not eof(binfile)) do begin
    blockread(binfile,readbyte,1,dummy);

    writeln(textfile,ConstructBitString(readbyte));
  end;

  close(binfile);
  close(textfile);
end.

ConstructBitString is defined as a function which returns a string, and is just composed,basically, of the code I presented above for making a bitstring from a byte.

Any more questions?  I hope you can see the process to take a text file and convert it back into a binary one...

Happy programming!  :)
0
 
LVL 1

Author Comment

by:potsy
ID: 1216957
The first part fo converting the bytes doesn't give any output and the second one gives me an invalid file type on this line blockread(binfile,readbyte,1,dummy);
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216958
OK, I tested this...

uses crt,dos;
var curbyte:byte;
    dummy:word;
    textfile:text;
    f:file;


function ConvertToBitString(val:byte):string;
var i:byte;
    bitstring:string;
begin
  bitstring:='';
  for i:=7 downto 0 do
    if (val and (1 shl i)=(1 shl i)) then
      bitstring:=bitstring+'1' else bitstring:=bitstring+'0';

  ConvertToBitString:=bitstring;
end;

begin
  assign(textfile,'s:\win311\fuck.txt');
  assign(f,'s:\win311\winver.exe');

  rewrite(textfile);
  reset(f,1);

  while (not eof(f)) do begin
    blockread(f,curbyte,1,dummy);
    writeln(textfile,ConvertToBitString(curbyte));
  end;


  close(textfile);
  close(f);
end.
0
 
LVL 1

Author Comment

by:potsy
ID: 1216959
one more thing and you will have you points I need the part to turn the binary back in to a useable file.
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216960
Think about it for a second.  Do you think you can do it yourself?  I will supply code if needed, but I think you could do it yourself (it woudl be a learning experience!).  Just read in one line at a time from the textfile, convert the bitstring BACK to a number, then use blockwrite to write back one byte to the exe file....  ???  Try it yourself, and let me know ifyou need more help...

(Could you possibly up the point value for the question, since I have provided much help and much code?)
0
 
LVL 1

Author Comment

by:potsy
ID: 1216961
Adjusted points to 125
0
 
LVL 1

Author Comment

by:potsy
ID: 1216962
well I have this much but I can't figure out how to convert the binary back to bytes of information

var
  textfile, f: file;
  NumRead, NumWritten: Word;
  Buf: array[1..2048] of Char;
begin
  assign(textfile,'c:\tpascal\steven\test3.txt');
  assign(f,'c:\tpascal\steven\menu.exe');
  reset(textfile,1);
  rewrite(f,1);
  repeat
    BlockRead(textfile, Buf, SizeOf(Buf), NumRead);
    BlockWrite(f, Buf, NumRead, NumWritten);
  until (NumRead = 0) or (NumWritten <> NumRead);
  Close(textfile);
  Close(f);
end.
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216963
OK, you have started down the right track.  Make the variable textfile of type text, and use readln, rather than blockread.  Also, if you use a repeat until, or a while... use the loop invariant as while (not eof(textfile)) do...  Plus you will need a function which will take a string and chunk out a number.  Here is some untested code:

var textfile:text;
    f:file;
    str:string;
    dummy:word;
begin
  assign(f,'blah.exe');
  assign(textfile,'text.txt');

  reset(textfile);
  rewrite(f,1);  {Set up record size in file f to 1 byte}

  while (not eof(textfile)) do begin
    readln(textfile,str);
    if (ValidInput(str)) then
      blockwrite(f,ConvertBintoByte(str),1,dummy);
  end;

  close(textfile);
  close(f);
end;

see, when you declare a variable of type file, you need to also declare its record size.  This is done in the reset or rewrite.  So, rewrite(f,1) makes the record size 1 byte.  Then, in block read or blockwrite, the 3rd variable is the number of records to read from or write to.  This is why blockread is at times advantageous to readln using a textfile.  Where a textfile can read in only a byte or line at a time, you could set up blockread to read in 1024 bytes, or 500 records, or whatever.

Now, for the algorithm to take an 8 character bit string and turn it back into a number:

{untested}
function ConvertBittoByte(str:string):byte;
var i,result:byte;
begin
  result:=0;

  for i:=7 downto 0 do
    if (str[8-i]='1') then inc(result,(1 shl i));

  ConvertBittoByte:=result;
end;


Finally, the ValidInput function is just a function that you could write which takes a string input and outputs a boolean, which just does a little test to make sure that the string is 8 characters of 1's or 0's.

Happy programming!  :)
     
0
 
LVL 1

Author Comment

by:potsy
ID: 1216964
I'm having truble with the code this line seems to have many problems blockwrite(f,ConvertBintoByte(str),1,dummy);
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216965
uses crt;
var f:file;
    textfile:text;
    dummy:word;
    byteread:byte;
    mystr:string;

function Convert2Byte(str:string):byte;
var i,result:byte;
begin
  result:=0;
  for i:=7 downto 0 do
    if (str[8-i]='1') then inc(result,(1 shl i));

  Convert2Byte:=result;
end;

begin
  assign(textfile,'s:\win311\winver.txt');
  assign(f,'s:\win311\winver2.exe');

  rewrite(f,1);
  reset(textfile);

  while (not eof(textfile)) do begin
    readln(textfile,mystr);
    byteread:=Convert2Byte(mystr);
    blockwrite(f,byteread,1,dummy);
  end;

  close(textfile);
  close(f);
end.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question