From: "Björn Lundin" <b.f.lundin@gmail.com>
Subject: Re: Examining individual bytes of an integer
Date: Mon, 15 Oct 2018 12:52:27 +0200
Date: 2018-10-15T12:52:27+02:00 [thread overview]
Message-ID: <pq1rtn$54b$1@dont-email.me> (raw)
In-Reply-To: <g2hpbjFpnrsU1@mid.individual.net>
On 2018-10-14 23:04, Niklas Holsti wrote:
>
> (I have become very sensitive to endianness problems because my daily
> work involves writing Ada programs targeted to big-endian SPARC
> machines, but tested initially on little-endian PCs.)
I usually decide how to work with the bytes (little- or bigEndian)
and implement something like this
type Byte is range 0..255;
for Byte'Size use 8;
type Byte_Array is array (Positive range <>) of Byte;
subtype Byte_Array_2 is Byte_Array(1..2);
subtype Byte_Array_4 is Byte_Array(1..4);
procedure Swap4 (Bytes: in out Byte_Array_4) is
Result : Byte_Array_4;
begin
Result := (1 => Bytes(4), 2 => Bytes(3),
3 => Bytes(2), 4 => Bytes(1));
Bytes := Result;
end Swap4;
-------------------------------------------------
procedure Swap2 (Bytes: in out Byte_Array_2) is
Result : Byte_Array_2;
begin
Result := (1 => Bytes(2), 2 => Bytes(1));
Bytes := Result;
end Swap2;
-------------------------------------------------
function Mirror (Data : Byte_Array) return Byte_Array is
begin
case Data'Length is
when 2 =>
declare
Tmp : Byte_Array_2 := Data;
begin
case System.Default_Bit_Order is
when System.High_Order_First => null; --ppc
when System. Low_Order_First => Swap2(Tmp); --x86
end case;
return Tmp;
end;
when 4 =>
declare
Tmp : Byte_Array_4 := Data;
begin
case System.Default_Bit_Order is
when System.High_Order_First => null; --ppc
when System. Low_Order_First => Swap4(Tmp); --x86
end case;
return Tmp;
end;
when others => raise Constraint_Error with
"not implemented lenght" & Data'Length'img;
end case;
end Mirror;
-------------------------------------------------
Then I call Mirror once (at input),
and have the bytes in the same order no matter endianess.
The I can modify the bytes as I please.
I also call Mirror again at output, so I can write the
modifications in a machine friendly way.
(code is taken from project, but altered here without compilation,
especially the 4byte part)
--
--
Björn
next prev parent reply other threads:[~2018-10-15 10:52 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-14 19:15 Examining individual bytes of an integer Henrik Härkönen
2018-10-14 19:55 ` Jeffrey R. Carter
2018-10-14 21:28 ` Niklas Holsti
2018-10-15 10:18 ` AdaMagica
2018-10-15 18:43 ` Niklas Holsti
2018-10-15 20:18 ` Randy Brukardt
2018-10-16 10:18 ` AdaMagica
2018-10-16 11:55 ` Dmitry A. Kazakov
2018-10-16 22:35 ` Randy Brukardt
2018-10-17 8:10 ` Dmitry A. Kazakov
2018-10-17 9:47 ` briot.emmanuel
2018-10-17 10:04 ` Dmitry A. Kazakov
2018-10-17 21:57 ` Randy Brukardt
2018-10-16 22:33 ` Randy Brukardt
2018-10-17 6:51 ` Niklas Holsti
2018-10-17 22:01 ` Randy Brukardt
2018-10-17 8:17 ` Dmitry A. Kazakov
2018-10-17 22:03 ` Randy Brukardt
2018-10-17 22:10 ` Randy Brukardt
2018-10-18 8:01 ` Dmitry A. Kazakov
2018-10-18 9:33 ` AdaMagica
2018-10-18 21:18 ` Randy Brukardt
2018-10-19 6:27 ` Niklas Holsti
2018-10-19 8:28 ` AdaMagica
2018-10-19 16:38 ` Niklas Holsti
2018-10-14 21:04 ` Niklas Holsti
2018-10-15 7:49 ` Niklas Holsti
2018-10-15 8:55 ` Simon Wright
2018-10-15 10:52 ` Björn Lundin [this message]
2018-10-14 23:42 ` Matt Borchers
2018-10-14 23:45 ` Matt Borchers
2018-10-15 9:02 ` Simon Wright
2018-10-15 7:29 ` Dmitry A. Kazakov
2018-10-15 9:05 ` Simon Wright
2018-10-15 16:11 ` Simon Wright
2018-10-15 20:27 ` Randy Brukardt
2018-10-15 5:37 ` Henrik Härkönen
2018-10-15 7:42 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox