简体   繁体   English

无法从 C# 中的 HEX 获取负浮点值

[英]Cannot get negative float value from HEX in C#

I have to read some hex data from a file.我必须从文件中读取一些十六进制数据。 It works in C++ and Matlab but not in C# (which is the language I'm using).它适用于 C++ 和 Matlab 但不适用于 C# (这是我正在使用的语言)。

One data is made of 4 bytes, per exemple: d0bd a7fa一个数据由 4 个字节组成,例如: d0bd a7fa

In C++/Matlab I can get the right value: -1.3362在 C++/Matlab 中,我可以获得正确的值:-1.3362

But in C# I get this: 62.663810729980469但在 C# 我得到这个:62.663810729980469

Here here's the C# code I'm using:这是我正在使用的 C# 代码:

int exp = 6;
b.BaseStream.Seek(pos, SeekOrigin.Begin);
uint getValAbs = b.ReadUInt32();
(the below formula comes from the file format)
float actualValue = (float)(getValAbs * (Math.Pow(2, exp - 32)));
actualValue = 62.663810729980469;

and in matlab its并在 matlab 中

Spec(:,isub)    = fread(fid,fnpts,'long') * pow2(subfexp-32);

and it gives me the right value...它给了我正确的价值......

Any idea?任何想法?

The error is here:错误在这里:

b.ReadUInt32();

They are using 'long' that is signed , so他们正在使用已签名'long' ,所以

b.ReadInt32();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM