首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将十个字节转换为浮点数

如何将十个字节转换为浮点数
EN

Stack Overflow用户
提问于 2019-03-11 23:36:00
回答 1查看 301关注 0票数 0

我有一些遗留文件需要挖掘数据。这些文件是由用于DOS的Lotus123版本4创建的。我试图通过解析字节来更快地读取文件,而不是使用Lotus打开文件。我有每个10字节的值记录。

@AndrewMorton从这个Q指向http://www.mettalogic.co.uk/tim/l123/l123r4.html#rec17,它说我们有一个10字节的GNU Long Double。

当他们读取WK3文件时,他还指出了Gnumeric,但我很难找到相关的代码。

Dim fileBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(fiPath)
Dim arr(10) As Byte
For ...
    arr(x) = fileBytes(x)
    Debug.Print(Convert.ToInt16(fileBytes(x)))
Next ...

'values to the right
Dim data1 As Byte() = New Byte() {0, 0, 0, 0, 0, 45, 17, 188, 22, 64}    ' Value = 12325165
Dim data2 As Byte() = New Byte() {0, 0, 0, 0, 0, 248, 30, 196, 20, 64}   ' Value = 3213246
Dim data3 As Byte() = New Byte() {209, 92, 167, 145, 150, 202, 219, 205, 0, 64} ' Value = 3.21654
Dim data4 As Byte() = New Byte() {0, 0, 0, 0, 0, 120, 68, 196, 20, 64}   ' Value = 3215646
Dim data5 As Byte() = New Byte() {0, 0, 0, 0, 0, 104, 131, 211, 20, 192} ' Value = -3465434
Dim data6 As Byte() = New Byte() {0, 0, 0, 0, 0, 224, 131, 211, 20, 192} ' Value = -3465464
Dim data7 As Byte() = New Byte() {0, 0, 0, 0, 0, 60, 105, 163, 21, 192}  ' Value = -5354654
Dim data8 As Byte() = New Byte() {0, 0, 0, 0, 128, 82, 74, 135, 24, 192} ' Value = -35465546

Dim data1 As Byte() = New Byte() {0, 0, 0, 0, 0, 0, 0, 128, 255, 191}                  ' Value = -1
Dim data2 As Byte() = New Byte() {205, 204, 204, 204, 204, 204, 204, 204, 251, 191}    ' Value = -0.1
Dim data3 As Byte() = New Byte() {10, 215, 163, 112, 61, 10, 215, 163, 248, 191}       ' Value = -0.01
Dim data4 As Byte() = New Byte() {59, 223, 79, 141, 151, 110, 18, 131, 245, 191}       ' Value = -0.001
Dim data5 As Byte() = New Byte() {44, 101, 25, 226, 88, 23, 183, 209, 241, 191}        ' Value = -0.0001
Dim data6 As Byte() = New Byte() {35, 132, 71, 27, 71, 172, 197, 167, 238, 191}        ' Value = -0.00001
Dim data7 As Byte() = New Byte() {182, 105, 108, 175, 5, 189, 55, 134, 235, 191}       ' Value = -0.000001
Dim data8 As Byte() = New Byte() {0, 0, 0, 0, 0, 0, 0, 128, 255, 63}                   ' Value = 1
Dim data9 As Byte() = New Byte() {205, 204, 204, 204, 204, 204, 204, 204, 251, 63}     ' Value = 0.1
Dim data10 As Byte() = New Byte() {10, 215, 163, 112, 61, 10, 215, 163, 248, 63}       ' Value = 0.01
Dim data11 As Byte() = New Byte() {59, 223, 79, 141, 151, 110, 18, 131, 245, 63}       ' Value = 0.001
Dim data12 As Byte() = New Byte() {44, 101, 25, 226, 88, 23, 183, 209, 241, 63}        ' Value = 0.0001
Dim data13 As Byte() = New Byte() {35, 132, 71, 27, 71, 172, 197, 167, 238, 63}        ' Value = 0.00001
Dim data14 As Byte() = New Byte() {182, 105, 108, 175, 5, 189, 55, 134, 235, 63}       ' Value = 0.000001
Dim data15 As Byte() = New Byte() {188, 66, 122, 229, 213, 148, 191, 214, 231, 63}     ' Value = 0.0000001

我试图实现this,但它不起作用;只是给出了无用的值(两个答案我都试过了)。

如何将10字节GNU Long Double转换为Decimal。因为GNU是用C语言编写的,所以我会欢迎C代码,但我更喜欢VB.net。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-12 11:02:53

如果您的编译器的long double实现与此格式兼容,则使用C代码从十个十进制值构建一个十个字节的数组,然后将该数组解释为long double是很简单的。下面的程序使用在命令行中给定的十个小数。显然,可以直接从其他地方获取数字,比如从标准输入或从文件中获取。

#include <stdlib.h>
#include <stdio.h>

void
show_longdouble(const unsigned char array[]) {
    long double *ldp = (long double *)array;
    int ix;

    for (ix = 0 ; ix < 10 ; ++ix) {
        printf("%u ", array[ix]);
    }
    printf("=> %Lf\n", *ldp);
}

int
main(int argc, char *argv[])
{
    if (11 == argc) {
        unsigned char vals[16]; /* only use 10, but dimension 16 for alignment */
        int ix;

        for (ix = 0 ; ix < 10 ; ++ix) {
            sscanf(argv[ix+1], "%hhu", &vals[ix]);
        }
        show_longdouble(vals);
    }
    return 0;
}

运行此程序可实现以下功能:

$ ./gnu-long-double 0 0 0 0 0 45 17 188 22 64
0 0 0 0 0 45 17 188 22 64 => 12325165.000000

$ ./gnu-long-double 0 0 0 0 0 248 30 196 20 64
0 0 0 0 0 248 30 196 20 64 => 3213246.000000

如果您想手动进行转换,而不是依赖于C编译器的内置精度,那么这种格式在维基百科中被称为"x86扩展精度格式“,并在https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format中进行了描述

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55105407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档