How to Write Text data in Binary Format

A

aagarwal8

Hi,

I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...

private void btnWriteToFile_Click(object sender, EventArgs e)
{
FileStream fs = File.Open(@"D:\test.dat",
FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(txtTextToWrite.Text);
bw.Close();
fs.Close();
}

But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?

Please suggest how can i write strings in binary format? (or in a
format that is not human understandable.....please note i dont want to
use database or encryption for this purpose)

Regards
Ankit!!
 
J

Jon Skeet [C# MVP]

I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...

What exactly do you mean by "in binary format"?

But when i see the contents of the file, they are in clear text.

Yes, that's exactly what's meant to happen.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?

You're assuming BinaryWriter will do something, but I don't think
you're terribly clear on what it is.
Please suggest how can i write strings in binary format? (or in a
format that is not human understandable.....please note i dont want to
use database or encryption for this purpose)

Encryption is precisely the act of making something "not human
understandable". If the point is to stop people from reading the text,
then encryption is the way to go.
 
A

aagarwal8

"If the point is to stop people from reading the text, then encryption
is the way to go." .... not exactly
i have seen Yahoo Messenger's archive files, which are not really in
encrypted format....they seem to b in binary mode

any solution other than Encryption??

Regards,
Ankit!
 
A

Alberto Poblacion

I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...
[...]
But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?

You are terribly wrong.

*All* files are always binary, meaning that they contain a sequence of
ones and zeroes. That is the only thing that a computer can store in a file.
When a program opens a file, it interprets those ones and zeroes, and does
with them whatever the program knows how to do. The BinaryWriter is dumping
into the file the same sequence of ones and zeroes that the String had in
memory. When you say that "you see the contents of the file", I assume that
you are not looking at the ones and zeroes yourself, but rather you are
using a program to open the file, such as Notepad. Notepad happens to
understand the same sequences of ones and zeroes that .Net uses to store the
strings in memory, so that is why you "see" clear text. But the file IS
binary.
Please suggest how can i write strings in binary format? (or in a
format that is not human understandable.....please note i dont want to
use database or encryption for this purpose)

You don't want a binary format. You want a format that is not
understandable to humans, which is a different thing. In general, no format
is directly understandable to most humans (although some of us would be able
to read a hex dump of an ASCII file with a little bit of effort). However,
humans don't look at the ones and zeroes of the file; they always use a
program to look at the contents of the file. So, basically, you want a
format that can't be understood by a program. This means that you want to
use encryption, or if you don't need security, at least a non-standard
encoding.

If you want to use a non-standard encoding, you can do it quite easily
in .Net by storing the string in a byte array (use
System.Text.Encoding.GetBytes) and then performing some operation with those
bytes, such as XORing a constant value to all of them. You then write the
bytes to the file using your BinaryWriter. This file will not be "readable"
in any obvious way, but someone who wants to devote some effort to the task
will be able to figure out how to decode it and see the contents. If you
want to be safe against such efforts, you will have to resort to
cryptoghraphy, which is available to your .Net program through the classes
in the System.Security.Cryptography namespace.
 
P

Peter Duniho

"If the point is to stop people from reading the text, then encryption
is the way to go." .... not exactly

Yes, exactly. It's part of the definition of "encryption".
i have seen Yahoo Messenger's archive files, which are not really in
encrypted format....they seem to b in binary mode

What is it about those files that you see as desirable? Why do you prefer
to store your data in a non-human-readable format?

Usually, absent a specific security need, text data becomes unreadable as
a side-effect of solving some _other_ goal (for example, compressing it).
So far, you haven't stated any goal other than to simply render the text
unreadable.
any solution other than Encryption??

Not if your goal is to render the data unreadable. By definition, _some_
kind of encryption (even if it is something simple) would be required.

So, please explain in more detail what your specific goal is and what sort
of solution you want. Many of us could suggest a wide variety of methods
for making plain text unreadable, but without knowing _why_ this is a goal
-- that is, what specific end result to you hope to achieve -- it's
premature to suggest a specific method.

Pete
 
C

carnold

Hi,

I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...

private void btnWriteToFile_Click(object sender, EventArgs e)
{
FileStream fs = File.Open(@"D:\test.dat",
FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(txtTextToWrite.Text);
bw.Close();
fs.Close();
}

But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?

Please suggest how can i write strings in binary format? (or in a
format that is not human understandable.....please note i dont want to
use database or encryption for this purpose)

Regards
Ankit!!

Wow.
 
J

Jon Skeet [C# MVP]

"If the point is to stop people from reading the text, then encryption
is the way to go." .... not exactly
i have seen Yahoo Messenger's archive files, which are not really in
encrypted format....they seem to b in binary mode

Until you define exactly what you mean by "binary mode" it's impossible
to say what the archive files are doing. They could just be compressed,
for example.
any solution other than Encryption??

What do you have against encryption?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top