comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: how to "put" a binary structure? (newbie question!)
Date: Wed, 01 Feb 2006 19:59:58 +0100
Date: 2006-02-01T19:59:58+01:00	[thread overview]
Message-ID: <4297838.5ZkbbiKo5Q@linux1.krischik.com> (raw)
In-Reply-To: drqpnb$43l$00$1@news.t-online.com

Norbert Caspari wrote:

> Dear experts,
> 
> I like to print the content of a binary bit structure with the put
> statement but it doesn't work. I'm a newbie to Ada programming and
> therefore please excuse if my question might be silly or stupid. Maybe I
> misunderstood something.
> 
> Here is some test case I tried to compile:
> 
> --------------------snip--------------------------
> with Ada.Text_Io;
> with Ada.Integer_Text_Io;
> with Interfaces;
> use Interfaces;
> use Ada;
> 
> procedure Bittest is
>  
>    type Bit_T is
>          (Off,
>           On);
> 
>    type Byte is
>       record
>          Bit0 : Bit_T;
>          Bit1 : Bit_T;
>          Bit2 : Bit_T;
>          Bit3 : Bit_T;
>          Bit4 : Bit_T;
>          Bit5 : Bit_T;
>          Bit6 : Bit_T;
>          Bit7 : Bit_T;
>       end record;
>  
>    for Byte'Size use 8;
> 
>    pragma Pack(Byte);    -- the whole structure should fit in one byte
> 
>    package Byte_Io is new Text_Io.Integer_Io(Byte => Unsigned_8);
> --^^^^^^^^^
> -- This does not work, why???

1) Because the generic package Integer_Io has no parameter Byte
2) Because Unsigned_8 is a modular type and not an integer type.
2) Because Byte is a record type and not an integer type

Suggested Reading:

http://en.wikipedia.org/wiki/Strongly-typed_programming_language
http://en.wikibooks.org/wiki/Ada_Programming/Types

>    Xxx.Bit0 := off;
>    Xxx.Bit1 := on;
>    Xxx.Bit2 := off;
>    Xxx.Bit3 := off;
>    Xxx.Bit4 := on;
>    Xxx.Bit5 := on;
>    Xxx.Bit6 := off;
>    Xxx.Bit7 := off;
> 
> -- and of course this does not work too because the declaration
> -- of the child package above failed.
> 
>    Byte_IO.Put(Xxx.Bit0,
>       Width=>1,
>       Base=>2);
> 
> -- Is it possible to print the whole content of a record structure with
> one -- put statement?

Yes - but you have to write that Put statement yourself. For a user defined
type you need a user defined Put. Suggested Reading:

http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Overloading

> end Bittest;
> -------------------------snap----------------------
> 
> The compiler says
> 
> missing actual for instantiation of "Num"
> instantiation abandoned

> How can I correct the source code to print the values stored in the
> structure Xxx? Is it possible, to print the whole content of Xxx with just
> one put statement? Or alternatively how can I assign the content of Xxx to
> another variable of the type Unsigned_8 that I can use for output? Or is
> there maybe another way?
 
> Questions over questions ;-)

But you start of with the wrong questions.

1) What should your output look like? You see: Text_IO is for human readable
text. But there is no rule how your record should be displayed for humans.
There is only a rule for enums but not for records.

2) Why have you defined an enum representation?

>    for Bit_T use (
>       Off => 0,
>       On  => 1);

Since Text_IO is for human readable text it is not used. The compiler will
print "ON" and "OFF" because that is the representation for humans.

3) Why do you bit pack?

>    for Bit_T'Size use 1;
>  pragma Pack(Byte);

Humand readable text is never packed but allways written in full.

4) Are you actually using the right type of I/O? Maybe you need a form of
binary I/O. Suggested Reading:

http://en.wikibooks.org/wiki/Ada_Programming/Input_Output

For binary I/O there are rules on how to write a record. With binary I/O
representation clauses and bit packing are actually used.

Don't get me wrong - I don't make fun of you - I really don't know what you
want. From your question it is not clear: you speak of print but prepare
the data as for binary output.

Once you have decided if you aim for text or binary output we will help you
further.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



  parent reply	other threads:[~2006-02-01 18:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-01 17:04 how to "put" a binary structure? (newbie question!) Norbert Caspari
2006-02-01 18:23 ` Dmitry A. Kazakov
2006-02-01 18:34 ` Georg Bauhaus
2006-02-01 18:59 ` Martin Krischik [this message]
2006-02-02 11:06   ` Stephen Leake
2006-02-02  2:10 ` Jeffrey R. Carter
replies disabled

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