comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.Leake@gsfc.nasa.gov>
Subject: Re: Big-endian vs little-endian
Date: 1999/02/09
Date: 1999-02-09T00:00:00+00:00	[thread overview]
Message-ID: <ulni7e04i.fsf@gsfc.nasa.gov> (raw)
In-Reply-To: 36BB9A6E.D472A709@wvu.edu

Mike Werner <mwerner@wvu.edu> writes:

> Here's the relevant data structure:
> 
>    type Sys_type is (Zarya, Unity, PMA1, PMA2);
>    type Subsys_type is (CDH, CT, ECLSS, EPS, GNC, SM);
>    subtype Desc_type is String(1..256);
>    subtype Dur_Min_Type is Integer;
>    subtype Dur_Sec_type is Integer;
>    type Apm_Rec is 
>       record
> 	 Description : Desc_Type;      
> 	 System : Sys_Type;
> 	 Subsystem : Subsys_Type;
> 	 Dur_Min : Dur_Min_Type;
> 	 Dur_Sec : Dur_Sec_Type;  
>       end record;
> 
> The problematic parts were the Apm_Rec.Dur_Min and the Apm_Rec.Dur_Sec -
> all the others read in just fine.

You have a byte-endianness problem. System.Bit_Order address a
bit-endiannes problem. They are similar, but different.

The best Ada solution is to use streams to read the binary file. You
have to define your own Integer type (you should do this anyway, to
make sure it is the same size as the school's server Integer type!).
Then you can define the stream read and write functions to do byte
swapping.

If you're not up to streams (quite understandable :), you can just use
Unchecked_Conversion. Assuming 32 bit integers, do something like:

type Network_4_Bytes is record
    Hi_Byte : Interfaces.Unsigned_8;
    Byte_3 : Interfaces.Unsigned_8;
    Byte_2 : Interfaces.Unsigned_8;
    Low_Byte : Interfaces.Unsigned_8;
end record;
pragma Pack (Network_4_Bytes);
for Network_4_Bytes'size use 32; -- confirm size

function To_Network is new Unchecked_conversion
    (Source => Interfaces.Integer_32,
     Target => Network_4_Bytes);

Of course, to make your code portable to your school computer, you'll
have to hide this in a body, and set a compile-time flag to decide
whether to swap bytes or not. I define a package Endianness to handle
the compile-time flag.

Good luck!

-- Stephe




  parent reply	other threads:[~1999-02-09  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-29  0:00 Big-endian vs little-endian Mike Werner
1999-02-02  0:00 ` Nick Roberts
1999-02-03  0:00   ` Mark A Biggar
1999-02-06  0:00     ` Samuel T. Harris
1999-02-08  0:00       ` dennison
1999-02-08  0:00         ` Samuel T. Harris
1999-02-04  0:00   ` Richard D Riehle
1999-02-06  0:00   ` Mike Werner
1999-02-07  0:00     ` Matthew Heaney
1999-02-09  0:00     ` Stephen Leake [this message]
1999-02-10  0:00     ` Mike Werner
replies disabled

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