如何使用BinaryWriter写double值?
How to use BinaryWriter to write double value?
我有两个应用程序需要快速传递值,并且需要保留一些值(当我重新启动计算机时它仍然存在),所以我需要创建一个文件,现在我知道如何处理 int
:
using (BinaryWriter writer = new BinaryWriter(new FileStream(@"C:\TEST", FileMode.Open)))
{
writer.Write(0); //00 00 00 00
writer.Write(1); //01 00 00 00
writer.Write(2); //02 00 00 00
writer.Write(3); //03 00 00 00
writer.Write(int.MaxValue); //FF FF FF 7F
}
byte[] test = new byte[4];
using (BinaryReader reader = new BinaryReader(new FileStream(@"C:\TEST", FileMode.Open)))
{
reader.BaseStream.Seek(8, SeekOrigin.Begin);
reader.Read(test, 0, 4);
Console.WriteLine(BitConverter.ToInt32(test, 0)); //2
reader.BaseStream.Seek(16, SeekOrigin.Begin);
reader.Read(test, 0, 4);
Console.WriteLine(BitConverter.ToInt32(test, 0)); //2147483647
Console.Read();
}
但是double
怎么办?
就像
一样简单
writer.Write((double)int.MaxValue);
Write(Double) Writes an eight-byte floating-point value to the
current stream and advances the stream position by eight bytes
至于读书
reader.ReadDouble()
我有两个应用程序需要快速传递值,并且需要保留一些值(当我重新启动计算机时它仍然存在),所以我需要创建一个文件,现在我知道如何处理 int
:
using (BinaryWriter writer = new BinaryWriter(new FileStream(@"C:\TEST", FileMode.Open)))
{
writer.Write(0); //00 00 00 00
writer.Write(1); //01 00 00 00
writer.Write(2); //02 00 00 00
writer.Write(3); //03 00 00 00
writer.Write(int.MaxValue); //FF FF FF 7F
}
byte[] test = new byte[4];
using (BinaryReader reader = new BinaryReader(new FileStream(@"C:\TEST", FileMode.Open)))
{
reader.BaseStream.Seek(8, SeekOrigin.Begin);
reader.Read(test, 0, 4);
Console.WriteLine(BitConverter.ToInt32(test, 0)); //2
reader.BaseStream.Seek(16, SeekOrigin.Begin);
reader.Read(test, 0, 4);
Console.WriteLine(BitConverter.ToInt32(test, 0)); //2147483647
Console.Read();
}
但是double
怎么办?
就像
一样简单writer.Write((double)int.MaxValue);
Write(Double) Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes
至于读书
reader.ReadDouble()