Solved

binary

Posted on 1997-12-06
14
405 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
  • 7
  • 6
14 Comments
 

Expert Comment

by:brendanmc
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:potsy
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:potsy
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 125
0
 
LVL 1

Author Comment

by:potsy
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now