By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,974 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,974 IT Pros & Developers. It's quick & easy.

C# BinaryWriter: Displays same text for string

P: 71
Hi everyone, can someone please explain to me why does the strings will have the same text when saved in a file using a binarywriter? I mean when other types are saved usually it was converter in some other characters that makes the output file unreadable in notepad, but how come if the values to save are strings it still retain its own characters.

I hope someone can explain to me or maybe a link..thanks in advance
Dec 17 '08 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 221
This is normal behaviour and it's related to the encoding that the binary writer uses. The strings that binarywriter writes to the file are in binary - they are the bytes of the string encoded in the specified character encoding, the default of which is human readable. One of the constructors for BinaryWriter provides a parameter to specify different character encodings.

If you want to make the strings unreadable to the casual reader you can derive from BinaryWriter and implement your own character encoding.
To take it one step further, if the data is extremely sensitive you could use the CryptoStream class to encrypt it.
Dec 17 '08 #2

P: 71 I know,thanks a lot..
Dec 18 '08 #3

P: 71
Just want to know if there are difference of binarywriter from a filestream.writebyte?

While analyzing their output it just seems that it has the same. What should I use then to write binary files?
Dec 19 '08 #4

P: 71
sorry its a wrong question.
let me revise it..
What is the difference between binarywriter.write and filestream.write?

How can i see their effect? because as of now i am just having the same output both write methods..
Dec 19 '08 #5

Expert 100+
P: 221
You can use either. The main difference is that the BinaryWriter class is more flexible. You can initialise it using a specific encoding and it contains a number of overloads for the Write method that assist you in writing almost any value type you want to the stream.
Dec 19 '08 #6

P: 71
Thanks a lot.
Now its clear to me.. :)
Dec 22 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.