Friday 15 April 2011

c# - Converting two shorts to IEEE754 single precision float -



c# - Converting two shorts to IEEE754 single precision float -

i'm using modbus tcp read info of device. info single precision floating point integers (ieee754) stored 2 16 bit values. need convert 2 shorts float.

an example: conversion of ushort[] { 17253, 46620 } should yield 229.711365.

i think problem device uses big endian , i'm working on pc little endian. current approach is:

ushort[] received = new ushort[]{ 17253, 46620 }; byte[] asbyte = new byte[] { (byte)(received[1] % 256), (byte)(received[1] / 256), (byte)(received[0] % 256), (byte)(received[0] / 256), }; float result = bitconverter.tosingle(asbyte, 0);

i hoping there shorter way , maybe way works on big endian pc, too.

i don't understand why not work:

ushort[] received = new ushort[] { 17253, 46620 }; byte[] asbyte = new byte[4]; buffer.blockcopy(received, 0, asbyte, 0, 4); float result = bitconverter.tosingle(asbyte, 0);

c# converter endianness ieee ushort

No comments:

Post a Comment