comp.lang.ada
 help / color / mirror / Atom feed
* how to "put" a binary structure? (newbie question!)
@ 2006-02-01 17:04 Norbert Caspari
  2006-02-01 18:23 ` Dmitry A. Kazakov
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Norbert Caspari @ 2006-02-01 17:04 UTC (permalink / raw)


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);

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

   for Bit_T'Size use 1;

   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???

   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?

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 ;-)

Anyway, thanks a lot in advance for your help.

Best regards, Norbert




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to "put" a binary structure? (newbie question!)
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2006-02-01 18:23 UTC (permalink / raw)


On Wed, 1 Feb 2006 18:04:43 +0100, Norbert Caspari wrote:

> 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.

Structure is not an integer type. You have to choose between three options:

1. Record type (structure)

2. Array of bits, like array (Integer range 1..8) of Boolean or of Bit_T

3. Modular integer type (unsigned with bit-wise operations) mod 2**8,
Unsigned_8

> 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?

type Look_Up_Table is array (Unsigned_8) of Byte;
Translation_Table : constant Look_Up_Table :=
   (  (Off, Off, Off, Off, Off, Off, Off, Off),
      (Off, Off, Off, Off, Off, Off, Off, On),
      (Off, Off, Off, Off, Off, Off, On, Off),
      (Off, Off, Off, Off, Off, Off, On, On),
      ...
      (On, On, On, On, On, On, On, On)
   );
function To_Byte (X : Unsigned_8) return Byte is
begin
   return Translation_Table (X);
end To_Byte;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to "put" a binary structure? (newbie question!)
  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
  2006-02-02  2:10 ` Jeffrey R. Carter
  3 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2006-02-01 18:34 UTC (permalink / raw)


Norbert Caspari wrote:

> 
> 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?

In addition to Dmitry's suggestions, consider initialising an
entire aggregate (record, or array). This way the compiler can
check complete initialisation

You can use the predefined 'Image attribute function of Bit_T for
printing, as shown below.

   Xxx: Byte :=
     (Bit0 => off,
      Bit1 => on,
      Bit2 => off,
      Bit3 => off,
      Bit4 => on,
      Bit5 => on,
      Bit6 => off,
      Bit7 => off);

begin
   Text_IO.put_line("Bit 1 is " & Bit_T'image(Xxx.Bit1));
   -- etc.
end bittest;

-- Georg 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to "put" a binary structure? (newbie question!)
  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
  2006-02-02 11:06   ` Stephen Leake
  2006-02-02  2:10 ` Jeffrey R. Carter
  3 siblings, 1 reply; 6+ messages in thread
From: Martin Krischik @ 2006-02-01 18:59 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to "put" a binary structure? (newbie question!)
  2006-02-01 17:04 how to "put" a binary structure? (newbie question!) Norbert Caspari
                   ` (2 preceding siblings ...)
  2006-02-01 18:59 ` Martin Krischik
@ 2006-02-02  2:10 ` Jeffrey R. Carter
  3 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2006-02-02  2:10 UTC (permalink / raw)


Norbert Caspari wrote:

>    package Byte_Io is new Text_Io.Integer_Io(Byte => Unsigned_8); 
> --^^^^^^^^^
> -- This does not work, why???
> 
> The compiler says
> 
> missing actual for instantiation of "Num"
> instantiation abandoned

As the compiler tells you, Ada.Text_IO.Integer_IO has a generic formal parameter 
named "Num". You have not supplied an actual parameter for it.

In addition, you have supplied an actual for "Byte", but it has no such formal 
parameter.

But things are just starting. Integer_IO is for signed integer types:

type T is range Lo .. High;

Unsigned_8 is a modular type:

type Unsigned_8 is mod 2 ** 8;

Ada.Text_IO.Modular_IO is for modular types.

>    Byte_IO.Put(Xxx.Bit0,
>       Width=>1,
>       Base=>2);

But even with an instantiation of Modular_IO, this won't work, because Xxx.Bit0 
is not a modular type, it's an enumeration type. Maybe you want an instantiation 
of Ada.Text_IO.Enumeration_IO?

All scalar types have the attribute function 'Image that returns a String 
representation of the value:

Bit_T'Image (Xxx.Bit0);

'Value works the other way.

For your type, Bit_T'Image (Off) = "OFF"; Bit_T'Image (On) = "ON".

It seems you want to output '0' or '1'. In that case, why use an enumeration 
type? You could use an appropriate modular type:

type Bit_Value is mod 2;

I suggest you spend some time reading Annex A:

http://www.adaic.org/standards/95lrm/html/RM-A.html

especially A.10, "Text Input-Output":

http://www.adaic.org/standards/95lrm/html/RM-A-10.html

-- 
Jeff Carter
"My name is Jim, but most people call me ... Jim."
Blazing Saddles
39



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to "put" a binary structure? (newbie question!)
  2006-02-01 18:59 ` Martin Krischik
@ 2006-02-02 11:06   ` Stephen Leake
  0 siblings, 0 replies; 6+ messages in thread
From: Stephen Leake @ 2006-02-02 11:06 UTC (permalink / raw)


Martin Krischik <krischik@users.sourceforge.net> writes:

> Norbert Caspari wrote:
>
>> Dear experts,
>> 
>> <snip>
>> -- 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. 

Or you can have auto_text_io write it for you:

http://www.toadmail.com/~ada_wizard/ada/auto_text_io.html

I guess I should add that to the wiki.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-02-02 11:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2006-02-02 11:06   ` Stephen Leake
2006-02-02  2:10 ` Jeffrey R. Carter

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