将两个字节转换为C#中的IEEE-11073 16位SFLOAT [英] Converting two bytes to an IEEE-11073 16-bit SFLOAT in C#

查看:280
本文介绍了将两个字节转换为C#中的IEEE-11073 16位SFLOAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据IEEE-11073将两个字节的数组转换为SFloat格式。



我该怎么做?

我在这里回答我的问题。

  public float ToSFloat(byte [] value)
{
if(value.Length!= 2)
throw new ArgumentException ();
字节b0 =值[0];
字节b1 =值[1]; (b0)+((ToInt(b1)& 0x0F)< 8),12);(b)。
var exponent = unsignedToSigned(ToInt(b1)>> 4,4);

return(float)(mantissa * Math.Pow(10,exponent));



public int ToInt(字节值)
{
返回值& 0xFF的; ((unsigned&(1<< size-1))!


private int unsignedToSigned(int unsigned,int size)
{
if = 0)
{
unsigned = -1 *((1 <= size-1) - (unsigned&((1 <= size-1)-1)))
}
返回无符号;


解决方案

松散地基于 C ++实现Signove on GitHub 我已经在C# :

 字典< Int32,Single> reservedValues = new Dictionary< Int32,Single> {
{0x07FE,Single.PositiveInfinity},
{0x07FF,Single.NaN},
{0x0800,Single.NaN},
{0x0801,Single.NaN},
{0x0802,Single.NegativeInfinity}
};

单个Ieee11073ToSingle(Byte []字节){
var ieee11073 =(UInt16)(bytes [0] + 0x100 * bytes [1]);
var mantissa = ieee11073& 0x0FFF;
if(reservedValues.ContainsKey(mantissa))
return reservedValues [mantissa];
if(尾数> = 0x0800)
尾数= - (0x1000 - 尾数);
var exponent = ieee11073>> 12;
if(exponent> = 0x08)
exponent = - (0x10 - exponent);
var magnitude = Math.Pow(10d,exponent);
return(Single)(尾数*大小);





这个函数假定字节是小尾数格式。如果不是,你将不得不在函数的第一行交换 bytes [0] bytes [1] 。或者,甚至可以更好地从函数中删除第一行,并将函数参数更改为接受 UInt16 (IEEE 11073值),然后让调用者决定如何提取此值从输入。

我强烈建议你测试这个代码,因为我没有任何测试值来验证转换的正确性。


I need to convert a two byte array to SFloat format according to IEEE-11073.

How can I do that?

I answer my question here.

    public float ToSFloat(byte[] value)
    {
        if (value.Length != 2)
            throw new ArgumentException();
        byte b0 = value[0];
        byte b1 = value[1];


        var mantissa = unsignedToSigned(ToInt(b0) + ((ToInt(b1) & 0x0F) << 8), 12);
       var exponent = unsignedToSigned(ToInt(b1) >> 4, 4);

        return (float)(mantissa * Math.Pow(10, exponent));

    } 

    public int ToInt(byte value)
    {
        return value & 0xFF;
    }

    private int unsignedToSigned(int unsigned, int size) 
    {
        if ((unsigned & (1 << size-1)) != 0) 
     {
            unsigned = -1 * ((1 << size-1) - (unsigned & ((1 << size-1) - 1)));
        }        
        return unsigned;
    }

解决方案

Loosely based on the C implementation by Signove on GitHub I have created this function in C#:

Dictionary<Int32, Single> reservedValues = new Dictionary<Int32, Single> {
  { 0x07FE, Single.PositiveInfinity },
  { 0x07FF, Single.NaN },
  { 0x0800, Single.NaN },
  { 0x0801, Single.NaN },
  { 0x0802, Single.NegativeInfinity }
};

Single Ieee11073ToSingle(Byte[] bytes) {
  var ieee11073 = (UInt16) (bytes[0] + 0x100*bytes[1]);
  var mantissa = ieee11073 & 0x0FFF;
  if (reservedValues.ContainsKey(mantissa))
    return reservedValues[mantissa];
  if (mantissa >= 0x0800)
    mantissa = -(0x1000 - mantissa);
  var exponent = ieee11073 >> 12;
  if (exponent >= 0x08)
    exponent = -(0x10 - exponent);
  var magnitude = Math.Pow(10d, exponent);
  return (Single) (mantissa*magnitude);
}

This function assumes that the bytes are in little endian format. If not you will have to swap bytes[0] and bytes[1] in the first line of the function. Or perhaps even better remove the first line from the function and change the function argument to accept a UInt16 (the IEEE 11073 value) and then let the caller decide how to extract this value from the input.

I highly advise you to test this code because I do not have any test values to verify the correctnes of the conversion.

这篇关于将两个字节转换为C#中的IEEE-11073 16位SFLOAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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