comp.lang.ada
 help / color / mirror / Atom feed
From: sparky!steves@uunet.uu.net  (Steve Scheuber)
Subject: Checking byte order using unchecked_conversion
Date: 14 Sep 93 21:06:41 GMT	[thread overview]
Message-ID: <1993Sep14.210641.17606@sparky.sterling.com> (raw)

I'm posting this for a co-worker.  Please respond via e-mail.
Thank you.
-----------
I would like to know if the following code reliably detects byte order
using Unchecked_Conversion. (I don't have access to a big-endian machine.)
This correctly detected byte order on a DEC VAX using ACS Ada.

with System,
     Text_IO,
     Unchecked_Conversion;

procedure Byte_Order is
    Byte      : constant := System.Storage_Unit;
    Short_Int : constant := 2*Byte;

    type Bit8 is range 0 .. 2**Byte - 1;
    for Bit8'Size use Byte;

    type Bit16 is range 0 .. 2**Short_Int - 1;
    for Bit16'Size use Short_Int;

    type Bit16_Array is array(1..2) of Bit8;
    for Bit16_Array'Size use Short_Int;

    function Map is new Unchecked_Conversion(Source => Bit16,
                                             Target => Bit16_Array);

    The_Bit16    : constant Bit16 := 1;
    Mapped_Bit16 : constant Bit16_Array := Map(The_Bit16);

begin

    if Mapped_Bit16'First = 1 then
        Text_IO.Put_Line("Low byte order");
    elsif 
        Text_IO.Put_Line("High byte order");
    end if;
end Byte_Order;
-- 
Steve Scheuber                       NCYC Lives!
Software Engineering Process Group   INTERNET: steve_scheuber@sterling.com
Sterling Software, ITD               UUCP:     sparky!steve_scheuber
1404 Ft. Crook Rd. South             Phone:    (402) 291-8300 
Bellevue, NE. 68005-2969             FAX:      (402) 291-4362

                 reply	other threads:[~1993-09-14 21:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
replies disabled

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