We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# binary

on
Medium Priority
478 Views
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.
Comment
Watch Question

## View Solution Only

Commented:
it sounds like you would need to understand the specific format of the file.
Different binaries have different format

Commented:
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.

Commented:
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....

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
I would like source code because I'm trying to learn how to do this and I lern best by example

Commented:
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;
begin
assign(textfile,'somefile.txt');
assign(binfile,'whatever.exe');

rewrite(textfile);
reset(binfile);

while (not eof(binfile)) do begin

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!  :)

Commented:
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);

Commented:
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
writeln(textfile,ConvertToBitString(curbyte));
end;

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

Commented:
one more thing and you will have you points I need the part to turn the binary back in to a useable file.

Commented:
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?)

Commented:

Commented:
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;
Buf: array[1..2048] of Char;
begin
assign(textfile,'c:\tpascal\steven\test3.txt');
reset(textfile,1);
rewrite(f,1);
repeat
Close(textfile);
Close(f);
end.

Commented:
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
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!  :)

Commented:
I'm having truble with the code this line seems to have many problems blockwrite(f,ConvertBintoByte(str),1,dummy);

Commented:
uses crt;
var f:file;
textfile:text;
dummy:word;
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
end;

close(textfile);
close(f);
end.
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile