I want to be able to save several arrays of doubles to a file. I don’t want to use the binary serializer as like this work in environments where this isn’t available (i.e. Silverlight/WP7). I’d like this to I thought about using text format, which is easy, but I’m worried I’ll use accuracy when the numbers are rounded to开发者_如何学运维 be printed. So is there a ‘correct’ way to serialize a double to a text or binary format and retain full accuracy?
BinaryWrite.Write(double)
can be used safely to write (and then read) double. So you can loop around the array write the doubles. But as Jon said, you can also use BitConverter.GetBytes()
and manually write it.
Well, you could use the "r" (roundtrip) specifier to convert the double to a string, or you could use BitConverter.DoubleToInt64Bits to convert the double
to long
(which I assume you're confident in serializing accurately) and then BitConverter.Int64BitsToDouble
at the other end.
One problem - those methods aren't available in WP7. They're in desktop Silverlight though.
For Windows Phone 7 you could use BitConverter.GetBytes(double)
and BitConverter.ToDouble(byte[], int)
instead though.
精彩评论