C# BinaryWriter: Displays same text for string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantz
    New Member
    • Oct 2008
    • 71

    C# BinaryWriter: Displays same text for string

    Hi everyone, can someone please explain to me why does the strings will have the same text when saved in a file using a binarywriter? I mean when other types are saved usually it was converter in some other characters that makes the output file unreadable in notepad, but how come if the values to save are strings it still retain its own characters.

    I hope someone can explain to me or maybe a link..thanks in advance
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    This is normal behaviour and it's related to the encoding that the binary writer uses. The strings that binarywriter writes to the file are in binary - they are the bytes of the string encoded in the specified character encoding, the default of which is human readable. One of the constructors for BinaryWriter provides a parameter to specify different character encodings.

    If you want to make the strings unreadable to the casual reader you can derive from BinaryWriter and implement your own character encoding.
    To take it one step further, if the data is extremely sensitive you could use the CryptoStream class to encrypt it.

    Comment

    • dantz
      New Member
      • Oct 2008
      • 71

      #3
      oh..ok..now I know,thanks a lot..
      :)

      Comment

      • dantz
        New Member
        • Oct 2008
        • 71

        #4
        Followup: Difference of binarywriter vs filestream.writ ebyte

        Just want to know if there are difference of binarywriter from a filestream.writ ebyte?

        While analyzing their output it just seems that it has the same. What should I use then to write binary files?

        Comment

        • dantz
          New Member
          • Oct 2008
          • 71

          #5
          What is the difference between binarywriter.wr ite and filestream.writ e

          Originally posted by dantz
          Just want to know if there are difference of binarywriter from a filestream.writ ebyte?

          While analyzing their output it just seems that it has the same. What should I use then to write binary files?
          sorry its a wrong question.
          let me revise it..
          What is the difference between binarywriter.wr ite and filestream.writ e?

          How can i see their effect? because as of now i am just having the same output both write methods..

          Comment

          • nukefusion
            Recognized Expert New Member
            • Mar 2008
            • 221

            #6
            You can use either. The main difference is that the BinaryWriter class is more flexible. You can initialise it using a specific encoding and it contains a number of overloads for the Write method that assist you in writing almost any value type you want to the stream.

            Comment

            • dantz
              New Member
              • Oct 2008
              • 71

              #7
              Thanks a lot.
              Now its clear to me.. :)

              Comment

              Working...