From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,PLING_QUERY autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5ffde75d5ac5a4c2 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news3.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!kanaga.switch.ch!news-zh.switch.ch!switch.ch!news.hispeed.ch!linux2.krischik.com!news From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: how to "put" a binary structure? (newbie question!) Date: Wed, 01 Feb 2006 19:59:58 +0100 Organization: Cablecom Newsserver Message-ID: <4297838.5ZkbbiKo5Q@linux1.krischik.com> References: NNTP-Posting-Host: 84-74-134-212.dclient.hispeed.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.hispeed.ch 1138820403 9103 84.74.134.212 (1 Feb 2006 19:00:03 GMT) X-Complaints-To: news@hispeed.ch NNTP-Posting-Date: Wed, 1 Feb 2006 19:00:03 +0000 (UTC) User-Agent: KNode/0.10 Xref: g2news1.google.com comp.lang.ada:2738 Date: 2006-02-01T19:59:58+01:00 List-Id: 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