2016-06-11 12 views
3

I haben einen Netzwerk-Array von 2 Bytes, die ich brauche [Werte zwischen -1 ... 1-2.E (-15)]
Beispiele zu schweben zu konvertieren:convert Byte-Array auf 16 Bit float

byte[] Arr1={0x70 , 0x54} //==> Result = 0.660 
byte[] Arr2={0x10 , 0x37} //==> Result = 0.430 

Irgendwelche Lösungen, um dies zu überbrücken?

+0

[Diese stackoverflow Antwort] (http://stackoverflow.com/questions/2619664/c-convert-byte-array-into-a-float) sollte helfen. Die Antwort enthält die Verwendung der Klasse [System.BitConverter] (https://msdn.microsoft.com/en-us/library/system.bitconverter (v = vs.110) .aspx). –

+0

@ yeah-buddy Ich habe gesehen, dass diese Methode 'ToSingle (byte [] value, int startIndex)' ein Array von mindestens 4 Bytes erhält, wenn ich dieses 'byte [] Array = {0x70, 0x54, 0x00, 0x00}; Float myFloat = System.BitConverter.ToSingle (Array, 0); // float = 3,02E-41 'was weit entfernt vom korrekten Ergebnis ist –

Antwort

3

Welchen Standard haben Sie verwendet, {0x70 , 0x54}?

Ich habe einen Beispielcode für Half-Precision Fließkomma-Konversation nach IEEE 754-2008 Standard gemacht.
https://en.wikipedia.org/wiki/Half-precision_floating-point_format

public static float toTwoByteFloat(byte HO, byte LO) 
{ 
    var intVal = BitConverter.ToInt32(new byte[] { HO, LO, 0, 0 }, 0); 

    int mant = intVal & 0x03ff; 
    int exp = intVal & 0x7c00; 
    if (exp == 0x7c00) exp = 0x3fc00; 
    else if (exp != 0) 
    { 
     exp += 0x1c000; 
     if (mant == 0 && exp > 0x1c400) 
      return BitConverter.ToSingle(BitConverter.GetBytes((intVal & 0x8000) << 16 | exp << 13 | 0x3ff), 0); 
    } 
    else if (mant != 0) 
    { 
     exp = 0x1c400; 
     do 
     { 
      mant <<= 1; 
      exp -= 0x400; 
     } while ((mant & 0x400) == 0); 
     mant &= 0x3ff; 
    } 
    return BitConverter.ToSingle(BitConverter.GetBytes((intVal & 0x8000) << 16 | (exp | mant) << 13), 0); 
} 

private static byte[] I2B(int input) 
{ 
    var bytes = BitConverter.GetBytes(input); 
    return new byte[] { bytes[0], bytes[1] }; 
} 

public static byte[] ToInt(float twoByteFloat) 
{ 
    int fbits = BitConverter.ToInt32(BitConverter.GetBytes(twoByteFloat), 0); 
    int sign = fbits >> 16 & 0x8000; 
    int val = (fbits & 0x7fffffff) + 0x1000; 
    if (val >= 0x47800000) 
    { 
     if ((fbits & 0x7fffffff) >= 0x47800000) 
     { 
      if (val < 0x7f800000) return I2B(sign | 0x7c00); 
      return I2B(sign | 0x7c00 | (fbits & 0x007fffff) >> 13); 
     } 
     return I2B(sign | 0x7bff); 
    } 
    if (val >= 0x38800000) return I2B(sign | val - 0x38000000 >> 13); 
    if (val < 0x33000000) return I2B(sign); 
    val = (fbits & 0x7fffffff) >> 23; 
    return I2B(sign | ((fbits & 0x7fffff | 0x800000) + (0x800000 >> val - 102) >> 126 - val)); 
} 

Sie werden es wie

folgende verwenden
private void button1_Click(object sender, EventArgs e) 
{ 
    var x = ToInt(0.660f); //it's 0x48 0x39 
    var y = toTwoByteFloat(x[0], x[1]); //it's 0.66015625 
} 
+0

Tatsächlich muss ich diese 2 Bytes in einen normalisierten Wert zwischen -1 und 1-2E umwandeln (-15) –

0

Es wurde als dies beantwortet, wird der Wert des Bytes ein pourcentage ist, so ist es von 32767 Beispiel geteilt werden müssen: byte[] Arr1={0x70 , 0x54} //==> Result =0x5470/0x7fff= 0.660 byte[] Arr2={0x10 , 0x37} //==> Result = 0x3710/0x7fff= 0.430