[fpc-pascal] Converting big-endian signed 16bits values to fpc integer
Andrew Haines
AndrewD207 at aol.com
Mon Apr 16 01:44:08 CEST 2012
On 04/15/12 18:38, Giuliano Colla wrote:
> I'm dealing with a large number of data coming from an external device.
> They are big-endian 16 bits signed numbers, which must be converted to
> integer and to real to perform calculations.
>
> Besides the obvious solutions of treating them as isolated bytes,
> multipying by 256 the highest, adding the lower, and oring wit
> $FFFF0000, or whatever sizeof(Integer) suggests if the result is bigger
> than 32767, is there a more efficient and elegant way to achieve the
> result?
>
> I'm a bit lost on all available data types, and unable to tell apart the
> ones which are platform specific and the ones which are not, which
> assignments give the proper result and which do not.
>
> Any suggestion would be greatly appreciated.
>
> Giuliano Colla
>
> There exists the BEtoN and LEtoN functions you can use.
There exists the BEtoN and LEtoN functions you can use. (NtoLE and NtoBE
also)
var
Data: SmallInt; // 16 bit signed type
...
Data := GetBENumberFromWherever;
Data := BEtoN(Data);
I'm not sure about real types. If they are 32 bits and sent in two
pieces then you could just do
var
Data: Single; // 4 bytes
DataArray: array[0..1] of Word absolute Data;
...
// I'm not sure how the data is represented in memory for floats.
DataArray[1] := BEtoN(Word(GetBENumberFromWherever));
DataArray[0] := BEtoN(Word(GetBENumberFromWherever));
Anyway you get the idea. Probably you will have to experiment to achieve
exactly what you want. Someone else probably has a better answer.
Regards,
Andrew
More information about the fpc-pascal
mailing list