UShort containing Bits to int, float etc...

  • Thread starter Thread starter Marco [Stinger]
  • Start date Start date
M

Marco [Stinger]

Hi
Newbe question:

I've got an external (Embedded C+) function that reads a PLC and returns an
array of 16 unsigned (PLC people call them WORD, but in C# thay should
be UShort).
That array contains the bits required to valorize a variable of variuos
kinds:
can be a char, Byte, Short, UShort, Int, UInt and Float.

THA QUESTION IS:
How do I convert in CF C# the BITS (or BitArray) contained in thoose
UShort in a Float (etc....)

PLEEEEEESE Help

Ciao
Marco
 
That depends on the format they are using, endiannes, width of bitfields for
float point numbers etc. Easiest would be if you could obtain a C reference
for data conversion from them. It'll be relatively simple to convert it to
C#.
In general a handy tool for conversions between simple data types is
Buffer.BlockCopy:

double[] a = new double[] { 1, 2, 3 };

byte[] b = new byte[Buffer.ByteLength(a)];

Buffer.BlockCopy(a, 0, b, 0, Buffer.ByteLength(a));

This converts an array of doubles into their memory representation as bytes.
Both target and source can be any of the basic types, like int, short,
double, even char.
 
Back
Top