我如何把字节值成小数? [英] How do I convert byte values into decimals?

查看:241
本文介绍了我如何把字节值成小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件加载一些十进制值,但我不能工作了取原始数据,并将其转换为小数的正确方法。

I'm trying to load some decimal values from a file but I can't work out the correct way to take the raw values and convert them into decimals.

我读过出该文件到一个字节数组,四个字节每块应该代表一个十进制值。为了帮助看着办吧,我已经构建了怎样的十进制值1到46表示为4字节块表。

I've read the file out into a byte array, and each chunk of four bytes is supposed to represent one decimal value. To help figure it out, I've constructed a table of how the decimal values 1 through to 46 are represented as four byte chunks.

例如,数字1出现作为0,0,128,63数字2为0,0,0,64等高达46,这是0,0,56,66。完整的表,请 rel=\"nofollow\">。

For instance, the number 1 appears as 0,0,128,63 the number 2 as 0,0,0,64 and so on up to 46, which is 0,0,56,66. The full table is available here.

还有另外一个系列里面去到小数点后三位,包括底片的数字,它是的这里

There is also another series of numbers which go to three decimal places and include negatives, which is here.

我的状态

它们存储至少显著字节优先:1的,256的,65536的,16777216的。这使得进制序列01 01 00 00进数257(十进制)。在C / C ++,以阅读例如浮点数,这样做:浮动X; FREAD(安培; X,的sizeof(浮动),1,fileptr);

They are stored least significant byte first: 1's, 256's, 65536's, 16777216's. This makes the hex sequence 01 01 00 00 into the number 257 (decimal). In C/C++, to read e.g. a float, do: float x; fread(&x, sizeof(float), 1, fileptr);

不过我使用.NET的File.ReadAllBytes方法,所以这是没有太大的帮助。如果任何人都可以花几分钟来看看实例文件,看看他们是否能发现一种方法,将值转换为小数,我会非常感激。

However I'm using .NET's File.ReadAllBytes method so this isn't much help. If anyone can spare a few minutes to look at the examples files and see if they can spot a way to convert the values to decimals I'd be most grateful.

推荐答案

您可以使用 BitConverter.ToSingle 的读取字节数组浮点值,因此要获得花车序列,你可以做这样的事情:

You can use BitConverter.ToSingle to read a float value from a byte array, so to get a sequence of floats, you could do something like this:

byte[] data = File.ReadAllBytes(fileName);
int count = data.Length / 4;
Debug.Assert(data.Length % 4 == 0);

IEnumerable<float> values = Enumerable.Range(0, count)
    .Select(i => BitConverter.ToSingle(data, i*4));

这篇关于我如何把字节值成小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆