473,661 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# BinaryWriter: Displays same text for string

71 New Member
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 3056
nukefusion
221 Recognized Expert New Member
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 New Member
oh..ok..now I know,thanks a lot..
:)
Dec 18 '08 #3
dantz
71 New Member
Just want to know if there are difference of binarywriter from a filestream.writ ebyte?

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 New Member
@dantz
sorry its a wrong question.
let me revise it..
What is the difference between binarywriter.wr ite and filestream.writ e?

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 Recognized Expert New Member
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 New Member
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
5136
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 starting at position 0. I thought that char was 2 bytes, but this makes it seem as though it is just 1 when I write to a file. Why is this? I imagine the extra bit is just a null bit (correct me if I'm wrong). I don't know if this helps but when I...
6
2994
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. The code calls the bw.write(TheValue), but nothing is written after 1kb. I'm I missing something? Here's the way I create my FileStream and BinaryWriter : Filename = System.Environment.CurrentDirectory() & "\msec.dat"
3
3817
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 writer = new BinaryWriter(myStream); writer.Write("ntestestse");
17
3794
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 object garbage collection after the WriteSomething method is executed? ---- using System; using System.IO;
1
2320
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 WriteUnicodeStringToStream (Stream s, string string) { BinaryWriter bw = new BinaryWriter (s, Encoding.Unicode); bw.Write (string); }
5
1500
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 shows the line perfect in the immediate windows without the carrot. Any help would be appreciated. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
3
3912
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() the resulting file 'Text.txt' has the same length than the string 'temp'.
9
8156
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() the resulting file 'Text.txt' has the same length than the string 'temp'.
2
2251
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 Code: int version = 1024; string loginname = "test";
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.