Davood
Davood

Reputation: 5645

Why binary data writing's result is string?

I want to create a binary file and store string data in it, I used this sample:

FileStream fs = new FileStream("c:\\test.data", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Encoding.ASCII.GetBytes("david stein"));
bw.Close();

but when I opened created file by this sample (test.data) in notepad, it has string data in it ("david stein"), now my question is that whats the difference between this binary writing and text writing when the result is string?

I'm looking to create a data in binary file until user can not open and read my data by note pad and if user open it in notepad see real binary data .

in some files when you open theme in text editors you can not read file content like jpg files contents,they do not use any encryption methods,what about it?how can i wite my data like this?

Upvotes: 2

Views: 625

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500635

now my question is that whats the difference between this binary writing and text writing when the result is string?

The data in a file is always "a sequence of bytes". In this case, the sequence of bytes you've written is "the bytes representing the text 'david stein'" in the ASCII encoding. So yes, if you open the file in any editor which tries to interpret the bytes as text in a way which is compatible with ASCII, you'll see the text "david stein". Really it's just a load of bytes though - it all depends on how you interpret them.

If you'd written:

File.WriteAllText("c:\\test.data", "david stein", Encoding.ASCII);

you'd have ended up with the exact same sequence of bytes. There are any number of ways you could have created a file with the same bytes in. There's nothing about File.WriteAllText which "marks" the file as text, and there's nothing about FileStream or BinaryWriter which marks the file as binary.

EDIT: From comments:

I'm looking to create a data in binary file until user can not open and read my data by note pad

Well, there are lots of ways of doing that with different levels of security. Ideally, you'd want some sort of encryption - but then the code reading the data would need to be able to decrypt it as well, which means it would need to be able to get a key. That then moves the question to "how do I store a key".

If you only need to verify the data in the file (e.g. check that it matches something from elsewhere) then you could use a cryptographic hash instead.

If you only need to prevent the most casual of snoopers, you could use something which is basically just obfuscation - a very weak form of encryption with no "key" as such. Anyone who dceompiled your code would easily be able to get at the data in that case, but you may not care too much about that.

It all depends on your requirements.

Upvotes: 8

Samuel Neff
Samuel Neff

Reputation: 74909

All data is binary. A text file is binary data that happens to be a limited subset that represent valid characters, but it's still binary.

The way text editors typically differentiate a text file from a binary file is they scan a certain portion of the file for zero values, \0. These never exist in text-only files and almost always exist in binary files.

Upvotes: 1

Related Questions