Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-04

Converting big-endian signed 16bits values to fpc integer

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Giuliano Colla

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

Reply by email

Jeppe Græsdal Johansen
to Giuliano Colla

Just use the BEtoN function. That will convert whatever big-endian number to your native endian.

I don't know what speed it has, but it sure is elegant :)

Giuliano Colla wrote 23 lines
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 _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Jonas Maebe
to Giuliano Colla: I'm dealing with a large number of data coming from an external device. They are…
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.

Jonas

Reply by email

Giuliano Colla
to Jonas Maebe

Il 16/04/2012 00:42, Jonas Maebe ha scritto:

Show quoted text 9 lines
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.

I was sure that hidden somewhere there was a better way! Thanks a lot

Giuliano

Reply by email

Andrew Haines
to Giuliano Colla: I'm dealing with a large number of data coming from an external device. They are…
Giuliano Colla wrote 19 lines
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

Reply by email

Mark Morgan Lloyd
to Jonas Maebe: Call BEtoN() on them (Big Endian to Native byte order). Note that this routine is…
Jonas Maebe wrote 8 lines
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.

I asked this a few weeks ago in the context of doing endianness-changes in-place in a data structure, and Martin (I think) suggested overloading the := operator.

I did however find that one has to be extremely careful inside the custom operator, I ended up using Move() rather than an assignment to the result since it was tending to go recursive- some way of inhibiting all automatic type conversions would be useful.

(* This type represents a little-endian word, irrespective of the
platform. It  *)
(* is of known size (16 bits, tested by assertion) but is not directly
         *)
(* compatible with any other type.
         *)
//
type    WordLE= packed record b0, b1: byte; end;
(* Assign a little-endian word to an (unsigned) integer, with
appropriate       *)
(* conversion.
         *)
//
operator := (wle: WordLE): word;
begin
..
Type TAWSHeader=packed Record
        ThisSize: WordLE;
        PrevSize: WordLE;
        Flags:    Byte;
        Rsvd:     Byte;
      End;
Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Giuliano Colla
to Andrew Haines: There exists the BEtoN and LEtoN functions you can use. (NtoLE and NtoBE also) var Data:…

Il 16/04/2012 01:44, Andrew Haines ha scritto:

There exists the BEtoN and LEtoN functions you can use. (NtoLE and NtoBE also)

Thanks to everybody.
Now I believe to have enough information to proceed. I'll keep this little piece of discussion as an example to show when someone asks me why I prefer open source software to proprietary one!

Giuliano

Reply by email