tags:

views:

84

answers:

1

Hello,

I got this next problem. I have a binary file, which I write to it vital data of the system. One of the fields is time, which I use DateTime.Now.ToString("HHmmssffffff), in format of microseconds. This data (in a string) I convert (to ToCahrArray) (and checked it in debugging in it is fine), it consists of time valid till the microseconds. Then I write it and flush it to the file. When opening it with PsPad that translate binary to Ascii, I see that data is corrupted in this field and a nother one but the rest of the message is fine.

The code:

void Write(string strData) {
   char[] cD = strData.ToCharArry();
   bw.Write(c); //br is from type of BinaryWriter 
   bw.Flush();
}
+5  A: 

You're writing out the bytes in Unicode characters, not Ascii bytes. If you want Ascii bytes, you should change this to use the Encoding class.

byte[] data = Encoding.ASCII.GetBytes(strData);
bw.Write(data);

I strongly recommend reading Joel Spolsky's article on character sets and encoding. It may help you understand what your current code is not working properly.

Reed Copsey
+1 for the reference to Joel's article -- it should be required reading!
JMarsch