Re: How to Write Text data in Binary Format



<aagarwal8@xxxxxxxxx> wrote in message news:bfbcdbe4-6989-4430-9b57-fca732d0c1b5@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.Encoding.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.


.



Relevant Pages

  • /proc/<pid>/maps syntax
    ... Encourage better documentation of such formats ... We ended up with an inferior format in the long run. ... address and offset fields may or may not have leading zeroes, ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Number with leading zeros
    ... as there is no information on what the number of zeroes should be. ... >>Format is only part of presentation, ... >>Bob Phillips ... >>> I am copying numbers with leading zeros into excel. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Unexpected Result with "If" Logic/Conditional Statement
    ... with the "Special" Zip format, but I need to account for leading zeroes, ... check the actual outcome with the expected outcome. ... Computer Solutions ...
    (microsoft.public.excel.misc)
  • Re: formula application to cells
    ... Even though these characters are probably used as "parts numbers", ... very easily just format the column into a number format, ... And in the "Type" box enter the ten zeroes, ... I copied the formula to bottom of each column, ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Writing 32 bit integer to a file
    ... BinaryWriter writes strings in a length encoded format, ... then write the array of bytes if your string do not follow the ... BinaryWriter 'string' format. ... Floating point numbers are stored in 32 bit IEEE format. ...
    (microsoft.public.dotnet.languages.vb)