comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Does Ada support endiannes?
Date: Mon, 12 Dec 2011 12:23:53 -0700
Date: 2011-12-12T12:23:53-07:00	[thread overview]
Message-ID: <jc5kca$10lb$1@adenine.netfront.net> (raw)
In-Reply-To: <108d5437-ae36-4e79-ad4e-aab3b903d0f4@gl2g2000vbb.googlegroups.com>

On 12/12/2011 05:44 AM, Gerd wrote:
>
> Any suggestion on how to convert, others that "/" and "mod"?

Let's presume you're on a little-ending machine. Then you can write something like

type Byte is mod 2 ** 8;
for Byte'Size use 8;

type Native is record
    MSB   : Byte;
    MSB_1 : Byte;
    LSB_1 : Byte;
    LSB   : Byte;
end record;

for Native use record
    MSB   at 3 range 0 .. 7;
    MSB_1 at 2 range 0 .. 7;
    LSB_1 at 1 range 0 .. 7;
    LSB   at 0 range 0 .. 7;
end record;
for Native'Size use 32;

function From_Native is new Ada.Unchecked_Conversion
    (Source => Native, Target => Interfaces.Integer_32);
function To_Native is new Ada.Unchecked_Conversion
    (Source => Interfaces.Integer_32, Target => Native);

type Foreign is new Native;

for Foreign use record
    MSB   at 0 range 0 .. 7;
    MSB_1 at 1 range 0 .. 7;
    LSB_1 at 2 range 0 .. 7;
    LSB   at 3 range 0 .. 7;
end record;
for Foreign'Size use 32;

function From_Foreign is new Ada.Unchecked_Conversion
    (Source => Foreign, Target => Interfaces.Integer_32);
function To_Foreign is new Ada.Unchecked_Conversion
    (Source => Interfaces.Integer_32, Target => Foreign);

Now, if you have something in big-endian format in Value, you can convert it to 
little-ending by

From_Native (Native (To_Foreign (Value) ) )

which you would probably want to encapsulate

function Foreign_To_Native (Value : in Interfaces.Integer_32)
return Interfaces.Integer_32 is
    -- null;
begin -- Foreign_To_Native
    return From_Native (Native (To_Foreign (Value) ) );
end Foreign_To_Native;

You could also read your foreign value byte by byte, putting the bytes into the 
appropriate fields of a Native, then unchecked-convert that to an integer.

-- 
Jeff Carter
"Pray that there's intelligent life somewhere up in
space, 'cause there's bugger all down here on earth."
Monty Python's Meaning of Life
61

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---



  reply	other threads:[~2011-12-12 19:23 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12  8:57 Does Ada support endiannes? Gerd
2011-12-12  9:23 ` Niklas Holsti
2011-12-12 11:27 ` Dmitry A. Kazakov
2011-12-12 12:44   ` Gerd
2011-12-12 19:23     ` Jeffrey Carter [this message]
2011-12-13 14:25       ` Gerd
2011-12-13 14:19     ` Gautier write-only
2011-12-14 16:16       ` Gerd
2011-12-14 18:16         ` Dmitry A. Kazakov
2011-12-14 20:16         ` Gautier write-only
2011-12-15 11:27           ` Gerd
2011-12-15 13:01             ` Simon Wright
2011-12-15 13:37             ` Dmitry A. Kazakov
2011-12-15 20:12             ` Jeffrey Carter
2011-12-12 12:46   ` Gerd
2011-12-12 13:22     ` Dmitry A. Kazakov
2011-12-12 17:07       ` Charles H. Sampson
2011-12-12 18:33         ` Dmitry A. Kazakov
2011-12-14  5:19           ` Charles H. Sampson
2011-12-14  8:56             ` Dmitry A. Kazakov
2011-12-14  9:46               ` Simon Wright
2011-12-15  9:14               ` Charles H. Sampson
2011-12-15  9:46                 ` Dmitry A. Kazakov
2011-12-25 21:42                   ` Charles H. Sampson
2011-12-26 10:47                     ` Dmitry A. Kazakov
2011-12-27 10:38                       ` Georg Bauhaus
2011-12-27 12:35                         ` Dmitry A. Kazakov
2012-01-04  4:33                       ` Charles H. Sampson
2012-01-04 11:56                         ` Dmitry A. Kazakov
2011-12-12 13:33     ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox