comp.lang.ada
 help / color / mirror / Atom feed
From: Jeff Carter <carter@spam.innocon.com>
Subject: Re: Byte sex confusion
Date: 1997/05/09
Date: 1997-05-09T00:00:00+00:00	[thread overview]
Message-ID: <33733B21.59E2B600@spam.innocon.com> (raw)
In-Reply-To: 33716475.43A9@top.monad.net


Steven O'Neill wrote:
> 
> I have an interesting problem well known to anyone who has tried to
> develop on multiple platforms.  I have binary data that was recorded on
> a big-endian machine (SGI) which I am in need of reading on a
> little-endian machine (Intel).  The current method involves using C
> routines to fread the data into buffers and then casting the data into a
> variety of Ada records.
> 
> This is a dangerous practice (its not my code) but it works fine as long
> as the layout of the records remain the same and the underlying data
> formats are consistent.
> 
> What I'm looking for are ideas on how to access this data in an easy anc
> consistent fashion on both architectures...  Any ideas?
> 
> Thanks,
> Steve O'Neill

A simple solution is to convert everything into a standard
representation, and have portable conversion routines to and from that
representation.

It is easy to:

Use Unchecked_Conversion to convert your types into a type in Interfaces
that is unsigned and the same size as your type.

Use "and" and Shift_Right to extract the bytes from
Interfaces.Unsigned_X in LSB to MSB order.

Use Shift_Left and "or" to combine these bytes back into an
Interfaces.Unsigned_X.

These operations allow you to easily and portably, entirely in Ada,
create a packed array of Interfaces.Unsigned_8 that can be encoded on
one machine, sent to another, and decoded on the other machine. In
either direction. With the same source code.

This works with GNAT (I've done it Intel-Sparc). It also worked with
another compiler, but it had a deficient version of Interfaces (only
declared Unsigned_32), so I had to do all manipulation using Unsigned_32
instead of a size-appropriate modular type with shift operations.
-- 
Jeff Carter  PGP:1024/440FBE21
Auntie-spam reply to; try ( carter @ innocon . com )
"Now go away, or I shall taunt you a second time."
Monty Python & the Holy Grail




  parent reply	other threads:[~1997-05-09  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-05-07  0:00 Byte sex confusion Steven O'Neill
1997-05-08  0:00 ` Nick Roberts
1997-05-09  0:00   ` Roy Grimm
1997-05-09  0:00   ` Robert Dewar
1997-05-14  0:00     ` Steve Sciance
1997-05-09  0:00 ` Jeff Carter [this message]
1997-05-10  0:00   ` Robert Dewar
1997-05-11  0:00   ` Oliver Kellogg
1997-05-11  0:00   ` Matthew Heaney
replies disabled

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