BinaryWriter not converting strings to binary

T

testrasbath

Why strings are not getting converted to binary form, even if we use
BinaryWriter for writing to a file?

Which is the similar technique in c# as ifstream(filename,
ios::binary) in C++?

My Code:

int version = 1024;
string loginname = "test";
string password = "test";
FileStream fs = new FileStream("C:/test.bin",
FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(version);
bw.Write(loginname);
bw.Write(password);
bw.Close();
fs.Close();

The file can be viewed in notepad like this:
 testtest

the integer , 'version' is in binary form... but the strings are in
human readable form.

I want to to write all of the datas in a format which cannot be read
from a notepad.
Please somebody help me

chepps
 
A

Alberto Poblacion

Why strings are not getting converted to binary form, even if we use
BinaryWriter for writing to a file?
[...]
The file can be viewed in notepad like this:
 testtest

the integer , 'version' is in binary form... but the strings are in
human readable form.

This *IS* binary. The binary representation of a string is a copy of the
bytes that make up the string in the chosen encoding. This is actually a
sequence of ones and zeroes inside the file on disk. It is not encrypted,
meaning that any program that is aware of the encoding that was used to
represent the characters as ones and zeroes can read back those ones and
zeroes and display them as characters. This is what Notepad is doing when
you open the file inside it.
If you want to prevent this from happening, you need to choose a
"secret" encoding, that is, a combination of ones and zeroes that no one but
you knows how to convert back to characters. If you need real security, you
have to go beyond a simple secret encoding, and use some real cryptographic
operations. You can do that in .Net by means of a CryptoStream connected to
the FileStream that you are currently using. Look up "CryptoStream" in the
online manual to find some examples.
 
J

Jon Skeet [C# MVP]

Why strings are not getting converted to binary form, even if we use
BinaryWriter for writing to a file?

They are.
Which is the similar technique in c# as ifstream(filename,
ios::binary) in C++?

My Code:

int version = 1024;
string loginname = "test";
string password = "test";
FileStream fs = new FileStream("C:/test.bin",
FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(version);
bw.Write(loginname);
bw.Write(password);
bw.Close();
fs.Close();

The file can be viewed in notepad like this:
test test

the integer , 'version' is in binary form... but the strings are in
human readable form.

Yes, because the encoding used is still readable. That's to be
expected.
I want to to write all of the datas in a format which cannot be read
from a notepad.

In that case you should look at encryption.

Jon
 

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