Post by Martin DowieYou don't need to look up the RM (Annex B.2) wrt the
predefined modular types in package "Interfaces" and there
you will find Shift_Left/Shift_Right/etc routines. These are
defined as "Intrinsic", which means they are "built in" to the
compiler - i.e. it should have a blinking good idea of a good
representation of this on the target h/w.
Didn't know about that, thanks! That means if we want to output any
word to a stream in big endian we can do:
procedure Write_Big_Endian
(Stream : access Root_Stream_Type'Class;
W : in Word) is
A, B : Byte;
begin
A := Byte (Shift_Right (Unsigned_32(W), 8));
B := Byte (W and 16#00FF#);
Byte'Write (Stream, A);
Byte'Write (Stream, B);
end Write_Big_Endian;
and for little endian we can do:
procedure Write_Little_Endian
(Stream : access Root_Stream_Type'Class;
W : in Word) is
A, B : Byte;
begin
A := Byte (W and 16#00FF#);
B := Byte (Shift_Right (Unsigned_32(W), 8));
Byte'Write (Stream, B);
Byte'Write (Stream, A);
end Write_Little_Endian;
and this will work whatever the platform we compile on?