C# - 读取双精度值

发布于 2024-11-16 12:27:14 字数 127 浏览 4 评论 0原文

  1. 假设只有一个双精度值以二进制格式写入文件中。如何使用 C# 或 Java 读取该值?

  2. 如果我必须从一个巨大的二进制文件中找到一个双精度值,我应该使用什么技术来找到它?

  1. Suppose there is only one single double value written into a file in binary format. How can I read that value using C# or Java?

  2. If I have to find a double value from a huge binary file, what techniques should I use to find that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一抹苦笑 2024-11-23 12:27:14

Double 是 8 个字节。要从二进制文件中读取单个双精度数,您可以使用 BitConverter 类:

var fileContent = File.ReadAllBytes("C:\\1.bin");
double value = BitConverter.ToDouble(fileContent, 0);

如果需要从文件中间读取双精度数,请将 0 替换为字节偏移量。

如果不知道偏移量,则无法判断字节数组中的某个值是双精度型、整数还是字符串。

另一种方法是:

using (var fileStream = File.OpenRead("C:\\1.bin"))
using (var binaryReader = new BinaryReader(fileStream))
{
    // fileStream.Seek(0, SeekOrigin.Begin); // uncomment this line and set offset if the double is in the middle of the file
    var value = binaryReader.ReadDouble();
}

第二种方法更适合大文件,因为它不会将整个文件内容加载到内存中。

Double is 8 bytes. To read single double from binary file you can use BitConverter class:

var fileContent = File.ReadAllBytes("C:\\1.bin");
double value = BitConverter.ToDouble(fileContent, 0);

If you need to read double from the middle of the file, replace 0 with the byte offset.

If you don't know the offset, you can't possibly tell that certain value in the byte array is double, integer or string.

Another approach is:

using (var fileStream = File.OpenRead("C:\\1.bin"))
using (var binaryReader = new BinaryReader(fileStream))
{
    // fileStream.Seek(0, SeekOrigin.Begin); // uncomment this line and set offset if the double is in the middle of the file
    var value = binaryReader.ReadDouble();
}

The second approach is better for big files, as it does not load whole file content to the memory.

热血少△年 2024-11-23 12:27:14

您可以使用 BinaryReader 类。

double value;
using( Stream stream = File.OpenRead(fileName) )
using( BinaryReader reader = new BinaryReader(stream) )
{
    value = reader.ReadDouble();
}

对于第二点,如果您知道偏移量,只需使用 Stream.Seek 方法即可。

You can use the BinaryReader class.

double value;
using( Stream stream = File.OpenRead(fileName) )
using( BinaryReader reader = new BinaryReader(stream) )
{
    value = reader.ReadDouble();
}

For the second point, if you know the offset, simply use the Stream.Seek method.

伴我心暖 2024-11-23 12:27:14

看来我们需要知道 double 值是如何在文件中编码的,然后才能找到它。

It seems that we would need to know how the double value was encoded in the file before we could go about finding it.

香草可樂 2024-11-23 12:27:14

1)

        double theDouble;
        using (Stream sr = new FileStream(@"C:\delme.dat", FileMode.Open, FileAccess.Read))
        {
            byte[] buffer = new byte[8];
            sr.Read(buffer, 0, 8);

            theDouble = BitConverter.ToDouble(buffer, 0);
        }

2) 你不能。

1)

        double theDouble;
        using (Stream sr = new FileStream(@"C:\delme.dat", FileMode.Open, FileAccess.Read))
        {
            byte[] buffer = new byte[8];
            sr.Read(buffer, 0, 8);

            theDouble = BitConverter.ToDouble(buffer, 0);
        }

2) You can't.

向日葵 2024-11-23 12:27:14

以下是如何读取(和出于测试目的而写入)双精度数:

    // Write Double
    FileStream FS = new FileStream(@"C:\Test.bin", FileMode.Create);
    BinaryWriter BW = new BinaryWriter(FS);

    double Data = 123.456;

    BW.Write(Data);
    BW.Close();

    // Read Double
    FS = new FileStream(@"C:\Test.bin", FileMode.Open);
    BinaryReader BR = new BinaryReader(FS);

    Data = BR.ReadDouble();
    BR.Close();

从大文件中获取它取决于数据在文件中的布局方式。

Here's how to read (and write for testing purposes) a double:

    // Write Double
    FileStream FS = new FileStream(@"C:\Test.bin", FileMode.Create);
    BinaryWriter BW = new BinaryWriter(FS);

    double Data = 123.456;

    BW.Write(Data);
    BW.Close();

    // Read Double
    FS = new FileStream(@"C:\Test.bin", FileMode.Open);
    BinaryReader BR = new BinaryReader(FS);

    Data = BR.ReadDouble();
    BR.Close();

Getting it out of a large file depends on how the data is laid out in the file.

梦回旧景 2024-11-23 12:27:14
using (FileStream filestream = new FileStream(filename, FileMode.Open))
using (BinaryReader reader = new BinaryReader(filestream))
{
    float x = reader.ReadSingle();
}
using (FileStream filestream = new FileStream(filename, FileMode.Open))
using (BinaryReader reader = new BinaryReader(filestream))
{
    float x = reader.ReadSingle();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文