2

I am trying to write and read a binary file using c# BinaryWriter and BinaryReader classes. When I am storing a string in file, it is storing it properly, but when I am trying to read it is returning a string which has '\0' character on every alternate place within the string.

Here is the code:

 public void writeBinary(BinaryWriter bw)
 {
     bw.Write("Hello");
 }

 public void readBinary(BinaryReader br)
 {
     BinaryReader br = new BinaryReader(fs);
     String s;
     s = br.ReadString();
  }

Here s is getting value as = "H\0e\0l\0l\0o\0".

stuartd
  • 70,509
  • 14
  • 132
  • 163
Bosco
  • 3,835
  • 6
  • 25
  • 33
  • 4
    [The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](http://joelonsoftware.com/articles/Unicode.html) – GSerg Feb 16 '14 at 01:46
  • You need to account for the character encoding, as @CSerg indicated. All strings in .NET use UTF8 encoding (2 bytes per character), which supports many scripts (Roman, Japanese, Hebrew, etc). – Wagner DosAnjos Feb 16 '14 at 02:00

1 Answers1

4

You are using different encodings when reading and writing the file.

You are using UTF-16 when writing the file, so each character ends up as a 16 bit character code, i.e. two bytes.

You are using UTF-8 or some of the 8-bit encodings when reading the file, so each byte will end up as one character.

Pick one encoding and use for both reading and writing the file.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005