469,631 Members | 1,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

How to Write Text data in Binary Format

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!!
Dec 28 '07 #1
6 4935
<aa*******@gmail.comwrote:
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"?

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 28 '07 #2
"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!
Dec 28 '07 #3
<aa*******@gmail.comwrote in message
news:bf**********************************@e25g2000 prg.googlegroups.com...
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.
Dec 28 '07 #4
On Fri, 28 Dec 2007 01:22:35 -0800, <aa*******@gmail.comwrote:
"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
Dec 28 '07 #5
On Dec 28, 2:43 am, aagarw...@gmail.com wrote:
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.
Dec 28 '07 #6
<aa*******@gmail.comwrote:
"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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 28 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Eric | last post: by
7 posts views Thread by Hallvard B Furuseth | last post: by
1 post views Thread by =?Utf-8?B?U3RldmVU?= | last post: by
1 post views Thread by xiao | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.