473,698 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Write Text data in Binary Format

Hi,

I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...

private void btnWriteToFile_ Click(object sender, EventArgs e)
{
FileStream fs = File.Open(@"D:\ test.dat",
FileMode.OpenOr Create, FileAccess.Writ e);
BinaryWriter bw = new BinaryWriter(fs );
bw.Write(txtTex tToWrite.Text);
bw.Close();
fs.Close();
}

But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?

Please suggest how can i write strings in binary format? (or in a
format that is not human understandable. ....please note i dont want to
use database or encryption for this purpose)

Regards
Ankit!!
Dec 28 '07 #1
6 5236
<aa*******@gmai l.comwrote:
I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...
What exactly do you mean by "in binary format"?

<snip>
But when i see the contents of the file, they are in clear text.
Yes, that's exactly what's meant to happen.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?
You're assuming BinaryWriter will do something, but I don't think
you're terribly clear on what it is.
Please suggest how can i write strings in binary format? (or in a
format that is not human understandable. ....please note i dont want to
use database or encryption for this purpose)
Encryption is precisely the act of making something "not human
understandable" . If the point is to stop people from reading the text,
then encryption is the way to go.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 28 '07 #2
"If the point is to stop people from reading the text, then encryption
is the way to go." .... not exactly
i have seen Yahoo Messenger's archive files, which are not really in
encrypted format....they seem to b in binary mode

any solution other than Encryption??

Regards,
Ankit!
Dec 28 '07 #3
<aa*******@gmai l.comwrote in message
news:bf******** *************** ***********@e25 g2000prg.google groups.com...
I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...
[...]
But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?
You are terribly wrong.

*All* files are always binary, meaning that they contain a sequence of
ones and zeroes. That is the only thing that a computer can store in a file.
When a program opens a file, it interprets those ones and zeroes, and does
with them whatever the program knows how to do. The BinaryWriter is dumping
into the file the same sequence of ones and zeroes that the String had in
memory. When you say that "you see the contents of the file", I assume that
you are not looking at the ones and zeroes yourself, but rather you are
using a program to open the file, such as Notepad. Notepad happens to
understand the same sequences of ones and zeroes that .Net uses to store the
strings in memory, so that is why you "see" clear text. But the file IS
binary.
Please suggest how can i write strings in binary format? (or in a
format that is not human understandable. ....please note i dont want to
use database or encryption for this purpose)
You don't want a binary format. You want a format that is not
understandable to humans, which is a different thing. In general, no format
is directly understandable to most humans (although some of us would be able
to read a hex dump of an ASCII file with a little bit of effort). However,
humans don't look at the ones and zeroes of the file; they always use a
program to look at the contents of the file. So, basically, you want a
format that can't be understood by a program. This means that you want to
use encryption, or if you don't need security, at least a non-standard
encoding.

If you want to use a non-standard encoding, you can do it quite easily
in .Net by storing the string in a byte array (use
System.Text.Enc oding.GetBytes) and then performing some operation with those
bytes, such as XORing a constant value to all of them. You then write the
bytes to the file using your BinaryWriter. This file will not be "readable"
in any obvious way, but someone who wants to devote some effort to the task
will be able to figure out how to decode it and see the contents. If you
want to be safe against such efforts, you will have to resort to
cryptoghraphy, which is available to your .Net program through the classes
in the System.Security .Cryptography namespace.
Dec 28 '07 #4
On Fri, 28 Dec 2007 01:22:35 -0800, <aa*******@gmai l.comwrote:
"If the point is to stop people from reading the text, then encryption
is the way to go." .... not exactly
Yes, exactly. It's part of the definition of "encryption ".
i have seen Yahoo Messenger's archive files, which are not really in
encrypted format....they seem to b in binary mode
What is it about those files that you see as desirable? Why do you prefer
to store your data in a non-human-readable format?

Usually, absent a specific security need, text data becomes unreadable as
a side-effect of solving some _other_ goal (for example, compressing it).
So far, you haven't stated any goal other than to simply render the text
unreadable.
any solution other than Encryption??
Not if your goal is to render the data unreadable. By definition, _some_
kind of encryption (even if it is something simple) would be required.

So, please explain in more detail what your specific goal is and what sort
of solution you want. Many of us could suggest a wide variety of methods
for making plain text unreadable, but without knowing _why_ this is a goal
-- that is, what specific end result to you hope to achieve -- it's
premature to suggest a specific method.

Pete
Dec 28 '07 #5
On Dec 28, 2:43 am, aagarw...@gmail .com wrote:
Hi,

I am trying to write the contents of a textbox to a file in binary
format. My code looks like this...

private void btnWriteToFile_ Click(object sender, EventArgs e)
{
FileStream fs = File.Open(@"D:\ test.dat",
FileMode.OpenOr Create, FileAccess.Writ e);
BinaryWriter bw = new BinaryWriter(fs );
bw.Write(txtTex tToWrite.Text);
bw.Close();
fs.Close();
}

But when i see the contents of the file, they are in clear text.
BinaryWriter class doesnt seem to be working with strings....is that
the case or i am doing something terribly wrong here?

Please suggest how can i write strings in binary format? (or in a
format that is not human understandable. ....please note i dont want to
use database or encryption for this purpose)

Regards
Ankit!!
Wow.
Dec 28 '07 #6
<aa*******@gmai l.comwrote:
"If the point is to stop people from reading the text, then encryption
is the way to go." .... not exactly
i have seen Yahoo Messenger's archive files, which are not really in
encrypted format....they seem to b in binary mode
Until you define exactly what you mean by "binary mode" it's impossible
to say what the archive files are doing. They could just be compressed,
for example.
any solution other than Encryption??
What do you have against encryption?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 28 '07 #7

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

Similar topics

2
2038
by: Albert Tu | last post by:
Hi, I am learning and pretty new to Python and I hope your guys can give me a quick start. I have an about 1G-byte binary file from a flat panel x-ray detector; I know at the beggining there is a 128-byte header and the rest of the file is integers in 2-byte format. What I want to do is to save the binary data into several smaller files
3
3709
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw, projection1.raw, projection2.raw ... up to projection500.raw. Each file is 2*1024*768-byte big. I would like to read those files and convert to ascii files in %5.0f/n format as projection0.data ... projection500.data so that our
27
4996
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there any solid reasons to prefer text files over binary files files?
36
3589
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
4
2754
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself. the description is the following: the program will read a text file that contains historical price of a stock. The program will allow users to query...
7
3124
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following assumptions are, well, least unportable. In particular, do anyone know if there are real-life systems where the text file assumptions below don't hold? For text mode FILE*s,
1
3902
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how to properly serialize the System.Drawing.Icon structure to an XML file? The following code doesn't write the icon information to the xml file. private void CreateXmlFile(string filename) {
16
7239
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle Fairfield to accomplish the first step: Private Sub SaveObjectsAsText() path = CurrentProject.path & "\ObjectsAsText\" SaveDataAccessPagesAsText SaveFormsAsText SaveReportsAsText
1
2040
by: xiao | last post by:
HI~ guys , I have a program here (Sorry it is very long about 240 lines.) It can read and write the header information successfully but it cannot write the array successfully. I guess there is something wrong with the Write2DArrayInt function there. Can any one henlp me to find it out? Thank you~ (BTW : The printf function in line 114 and 139 implys that the data is right, but after that , in the new generated file, the values are all...
0
8672
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
9156
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...
1
8892
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
8860
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...
0
5860
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
4361
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...
1
3038
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
2323
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
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.