How to Write Text data in Binary Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aagarwal8@gmail.com

    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!!
  • Jon Skeet [C# MVP]

    #2
    Re: How to Write Text data in Binary Format

    <aagarwal8@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 - <skeet@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

    Comment

    • aagarwal8@gmail.com

      #3
      Re: How to Write Text data in Binary Format

      "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!

      Comment

      • Alberto Poblacion

        #4
        Re: How to Write Text data in Binary Format

        <aagarwal8@gmai l.comwrote in message
        news:bfbcdbe4-6989-4430-9b57-fca732d0c1b5@e2 5g2000prg.googl egroups.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.


        Comment

        • Peter Duniho

          #5
          Re: How to Write Text data in Binary Format

          On Fri, 28 Dec 2007 01:22:35 -0800, <aagarwal8@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

          Comment

          • carnold

            #6
            Re: How to Write Text data in Binary Format

            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.

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: How to Write Text data in Binary Format

              <aagarwal8@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 - <skeet@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

              Comment

              Working...