473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 5209
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there...
3
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw,...
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
36
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
1
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how...
16
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle...
1
by: xiao | last post by:
HI~ guys , I have a program here (Sorry it is very long about 240 lines.) It can read and write the header information successfully but it cannot write the array successfully. I guess there is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.