将二进制定点的 byte[] 转换为浮点值
我正在通过套接字读取一些数据。整数数据类型没有问题,System.BitConverter 方法可以正确处理转换。 (所以我认为不需要担心 Endian 问题?)
但是,BitConverter.ToDouble 不适用于数据的浮点部分......源规范对我来说有点低级别,但谈论了二进制定点表示形式,在较高有效方向上具有正字节偏移量,在较低有效方向上具有负字节偏移量。
我所做的大部分研究都是针对 C++ 或处理正弦和余弦的完整定点库,这听起来对于这个问题来说有点大材小用。有人可以帮助我使用 C# 函数从字节数组的 8 字节生成一个浮点数,例如 -3 字节偏移量吗?
根据要求提供格式的更多详细信息:
定点数据的带符号数值应使用二进制、补码表示法表示。对于定点数据,每个数据参数的值应相对于参考字节进行定义。参考字节定义一个八位字段,测量单位位于 LSB 位置。参考字节的LSB 值为1。 字节偏移量应由有符号整数定义,指示数据元素的最低有效字节相对于参考字节的位置。 数据元素的 MSB 代表符号位。 MSB 之间的位位置 参数绝对值和最高有效字节的 MSB 的值应等于符号位。
浮点数据应表示为符合 IEEE ANSI/IEEE Std 754-2008 的二进制浮点数。 (这句话来自不同的部分,可能是转移注意力)。
I'm reading some data over a socket. The integral data types are no trouble, the System.BitConverter methods are correctly handling the conversion. (So there are no Endian issues to worry about, I think?)
However, BitConverter.ToDouble isn't working for the floating point parts of the data...the source specification is a bit low level for me, but talks about a binary fixed point representation with a positive byte offset in the more significant direction and negative byte offset in the less significant direction.
Most of the research I've done has been aimed at C++ or a full fixed-point library handling sines and cosines, which sounds like overkill for this problem. Could someone please help me with a C# function to produce a float from 8 bytes of a byte array with, say, a -3 byte offset?
Further details of format as requested:
The signed numerical value of fixed point data shall be represented using binary, two's-complement notation.For fixed point data, the value of each data parameter shall be defined in relation to the reference byte. The reference byte defines an eight-bit field, with the unit of measure in the LSB position. The value of the LSB of the reference byte is ONE.
Byte offset shall be defined by a signed integer indicating the position of the least significant byte of a data element relative to the reference byte.
The MSB of the data element represents the sign bit. Bit positions between the MSB of the
parameter absolute value and the MSB of the most significant byte shall be equal in value to the sign bit.
Floating point data shall be represented as a binary floating point number in conformance with the IEEE ANSI/IEEE Std 754-2008. (This sentence is from a different section which may be a red herring).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,在向当地专家询问了一些有关源材料的问题后,事实证明 CodeInChaos 走在正确的轨道上...如果该值是 8 字节且偏移量为 -3 字节,那么我可以使用 BitConverter.ToInt64 / 256^ 3、如果是 4 字节且偏移量为 -1 字节,则 BitConverter.ToInt32 / 256 将产生正确的答案。我猜这意味着 XXX 签名的 BitConverter.ToXXX 足够聪明,可以处理二进制补码计算!
感谢那些试图提供帮助的人,我认为这不会太复杂,但是从参考文档措辞中获取 256 偏移量非常令人困惑:-)
Ok, after asking some questions from a local expert on the source material, it turns out CodeInChaos was on the right track...if the value is 8 bytes with a -3 byte offset, then I can use BitConverter.ToInt64 / 256^3, if it is 4 bytes with a -1 byte offset then BitConverter.ToInt32 / 256 will produce the correct answer. I guess that means BitConverter.ToXXX where XXX is signed is smart enough to handle the twos-complement calculations!
Thanks to those who tried to help out, I thought it couldn't be too complicated but getting that 256 offset from the reference document wording was very confusing:-)
System.BitConverter 工作速度非常慢,因此如果性能对您很重要,我建议您自己将字节转换为 int (通过逻辑移位)。
另外,请指定在您的协议中发送浮点数的确切格式。
System.BitConverter works very slow, so if performance is significant to you, i'd recommend to convert bytes to int by yourself (via logical shifts).
Also, please specify in what exact format floats are sent in your protocol.