473,386 Members | 1,705 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,386 software developers and data experts.

C# BinaryWriter: Displays same text for string

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
6 3041
nukefusion
221 Expert 100+
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
dantz
71
oh..ok..now I know,thanks a lot..
:)
Dec 18 '08 #3
dantz
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
dantz
71
@dantz
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
nukefusion
221 Expert 100+
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
dantz
71
Thanks a lot.
Now its clear to me.. :)
Dec 22 '08 #7

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

Similar topics

3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
6
by: ThunderMusic | last post by:
Hi, In my app, I open a file using a FileStream then pass it to a BinaryWriter. I then use the BinaryWriter instance to write to my file. But a problem arose : The file never gets bigger than 1kb....
3
by: Peyman | last post by:
Hello all, This is my first c# program. And I seem to have a problem. Here is the code: FileStream myStream = new FileStream("d:\\test.txt", FileMode.Create, FileAccess.Write); BinaryWriter...
17
by: Filip Strugar | last post by:
Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter...
1
by: Barguast | last post by:
Is it necessary to call the close method for every BinaryWriter that I create? Or is it just a way to close the underlying stream? For example is it OK to do the following: private void...
5
by: Travis Llewellyn | last post by:
I am writing a program that write out and array that I have stored. It writes out the whole array but adds a Carrot "^" in front of each section it writes. If I do a debug.writeline(hello) it...
3
by: philip | last post by:
If I execute that : Dim Temp as string = "This is a text" Dim sw As StreamWriter Dim fullFileName as string = "c:\text.txt" sw = New StreamWriter(fullFilename) sw.Write(temp) sw.Close() ...
9
by: philip | last post by:
If I execute that : Dim Temp as string = "This is a text" Dim sw As StreamWriter Dim fullFileName as string = "c:\text.txt" sw = New StreamWriter(fullFilename) sw.Write(temp) sw.Close() ...
2
by: testrasbath | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.