[fpc-pascal] Converting big-endian signed 16bits values to fpc integer
jonas.maebe at elis.ugent.be
Mon Apr 16 00:42:48 CEST 2012
On 16 Apr 2012, at 00: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?
Call BEtoN() on them (Big Endian to Native byte order). Note that this routine is overloaded for all integer sizes > 1, and that FPC extends the size of any number < 32/64 bits (depending on the architecture) to 32/64 bits when performing almost any operation on it. It's therefore probably the safest to call it as BEtoN(smallint(value)) to ensure that the correct overload is selected.
More information about the fpc-pascal