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

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

My Code:

int version = 1024;
string loginname = "test";
string password = "test";
FileStream fs = new FileStream("C:/test.bin",
FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(version);
bw.Write(loginname);
bw.Write(password);
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 2238
<te*********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.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 "CryptoStream" 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...@gmail.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(filename,
ios::binary) in C++?

My Code:

int version = 1024;
string loginname = "test";
string password = "test";
FileStream fs = new FileStream("C:/test.bin",
FileMode.OpenOrCreate);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(version);
bw.Write(loginname);
bw.Write(password);
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
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...
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...
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...
3
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...
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...
1
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...
3
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.