473,801 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BinaryWriter not converting strings to binary

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(filena me,
ios::binary) in C++?

My Code:

int version = 1024;
string loginname = "test";
string password = "test";
FileStream fs = new FileStream("C:/test.bin",
FileMode.OpenOr Create);
BinaryWriter bw = new BinaryWriter(fs );
bw.Write(versio n);
bw.Write(loginn ame);
bw.Write(passwo rd);
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

Mar 21 '07 #1
2 2255
<te*********@gm ail.comwrote in message
news:11******** **************@ n76g2000hsh.goo glegroups.com.. .
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 "CryptoStre am" in the
online manual to find some examples.
I want to to write all of the datas in a format which cannot be read
from a notepad.
Mar 21 '07 #2
On Mar 21, 11:07 am, testrasb...@gma il.com wrote:
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(filena me,
ios::binary) in C++?

My Code:

int version = 1024;
string loginname = "test";
string password = "test";
FileStream fs = new FileStream("C:/test.bin",
FileMode.OpenOr Create);
BinaryWriter bw = new BinaryWriter(fs );
bw.Write(versio n);
bw.Write(loginn ame);
bw.Write(passwo rd);
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

Mar 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2228
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert numbers into 16 bit binary form, so that I can eventually alter the LSB for dithering purposes. Obviously I can create my own function that will do this, but I was wondering if there was some quick process that I could call up from a library that...
6
3004
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
3830
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");
1
2323
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); }
3
1799
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods like so: Public Overloads Overrides Sub Write(ByVal Value As Byte) m_Queue.Enqueue(New WriteRequest(MyBase.BaseStream.Position, Value)) End Sub 'same for all other Write variants
5
1508
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
1
1973
by: heather.luiggi | last post by:
Is it possible to create an array of BinaryWriters? I need twenty BinaryWriters available to filter data into from a larger binary file. I would like to be able to access them by index rather than creating a large If statement. If is is possible to create an array of BinaryWriters, how do you do it? TIA!
3
6638
by: =?Utf-8?B?ZnVuZG9vX2ty?= | last post by:
Check the following code ///Buffer to store the data byte data = new byte; ///Create a memory stream from the buffer MemoryStream memStream = new MemoryStream(data); ///Binary writer BinaryWriter newData = new BinaryWriter(memStream);
1
2019
by: ShapeMan | last post by:
I'm trying to export data to a binary file. I have imported the data from a binary file using BinaryReader and everything works as I would have expected. The data after importing is a variety of mainly numerical types. I export it using BinaryWriter exactly the same way but get a much larger binary file. My research tells me that the Write method in BinaryWriter prefixes the bytes with the length. How can I avoid this? I'm quite new to...
0
9698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
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,...
0
10524
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10298
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7594
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
5619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4265
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.