Discussion:
Printing out an Array
(too old to reply)
c***@mac.com
2005-01-31 14:43:05 UTC
Permalink
Hello all, I'm new to this and comming from Java environment. I have
this code to convert bytes to bits but I dont know what to put in the
last line of code to get on the screen my converted number. Thanks for
any help
----------------------------------------------------------
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_text_Io; use Ada.Integer_text_Io;

procedure Pas3 is

subtype Octet is Integer range 0..255;
subtype Bit is Integer range 0..1;
type Tab_Bits is array (0 .. 7) of Bit;

function Octet_Vers_Bits (Oc : Octet )
return Tab_Bits is
Resultat : Tab_Bits;
N : Octet;

begin
N := Oc;
for I in reverse 0..7 loop
if ( N mod 2 = 1 ) then
Resultat(I) := 1;
else
Resultat(I) := 0;
end if;
N := N / 2;
end loop;
return Resultat;
end Octet_Vers_Bits;

t1 : Tab_bits;
x : integer;

begin
Put (" donner x: ");
Get(x);


for I in 0 .. 7 loop
t1 := Octet_Vers_Bits(x);
Put(Bit(x)); ----------------> here is the problem, what should I
put here.
end loop;
end Pas3 ;
Martin Krischik
2005-01-31 15:00:15 UTC
Permalink
Post by c***@mac.com
Hello all, I'm new to this and comming from Java environment. I have
this code to convert bytes to bits but I dont know what to put in the
last line of code to get on the screen my converted number. Thanks for
any help
----------------------------------------------------------
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_text_Io; use Ada.Integer_text_Io;
procedure Pas3 is
subtype Octet is Integer range 0..255;
subtype Bit is Integer range 0..1;
type Tab_Bits is array (0 .. 7) of Bit;
function Octet_Vers_Bits (Oc : Octet )
return Tab_Bits is
Resultat : Tab_Bits;
N : Octet;
begin
N := Oc;
for I in reverse 0..7 loop
if ( N mod 2 = 1 ) then
Resultat(I) := 1;
else
Resultat(I) := 0;
end if;
N := N / 2;
end loop;
return Resultat;
end Octet_Vers_Bits;
t1 : Tab_bits;
x : integer;
begin
Put (" donner x: ");
Get(x);
for I in 0 .. 7 loop
Read:

http://en.wikibooks.org/wiki/Programming:Ada:Control#for_loop_on_arrays

You will need tp wait for the next version of Java for that feature ;-).
Post by c***@mac.com
t1 := Octet_Vers_Bits(x);
Put(Bit(x)); ----------------> here is the problem, what should I
put here.
end loop;
end Pas3 ;
Two options open:

1) you can use the 'Image attribute to convert the integer to a string.

2) you can use Ada.Text_IO.Integer_IO to write the value.

Option 1 is quickly implemented but you can't format the value.
Option 2 is more difficult but you can use formating options.

You find wxamples for option 2 here:

http://en.wikibooks.org/wiki/Programming:Ada:Types:range

Martin
--
mailto://***@users.sourceforge.net
http://www.ada.krischik.com
Jeffrey Carter
2005-02-01 03:05:20 UTC
Permalink
Post by c***@mac.com
Hello all, I'm new to this and comming from Java environment. I have
this code to convert bytes to bits but I dont know what to put in the
last line of code to get on the screen my converted number. Thanks for
any help
Assuming this isn't homework, in the package Ada.Text_IO (ARM A.10.1)
you will find the generic packages Integer_IO and Modular_IO (described
in more detail in ARM A.10.8). If you look at the Put procedures, you
will find the parameter Base. This allows you to specify the number base
to represent the number in. If you supply the value 2 for Base, you will
get the number output in binary.

A typical value from Put with Base => 2 might be " 2#10101010#". If you
need to get rid of the leading " 2#" and the trailing '#', you can use
the Put that writes to a String and manipulate the result.
Alternatively, you can use an Image function from PragmARC.Images, which
does it for you.

So, in Ada it is not necessary to convert the value to an array of bits
(which could be accomplished with Ada.Unchecked_Conversion), nor to
output the bits individually. Yet another case where Ada is simpler and
more expressive than other languages.

The PragmAda Reusable Components are available from

http://home.earthlink.net/~jrcarter010/pragmarc.htm
--
Jeff Carter
"You couldn't catch clap in a brothel, silly English K...niggets."
Monty Python & the Holy Grail
19
Steve
2005-02-01 03:14:47 UTC
Permalink
Since the function Octet_Vers_Bits returns an array of Tab_Bits, no need to
call it inside the loop.
...
T1 := Octet_Vers_Bits(X);
for I in 0 .. 7 loop
Put(T1(I),1);
end loop;

Just index the array to get the value.

Also, I prefer:

Resultat(I) := N mod 2;

To:

if ( N mod 2 = 1 ) then
Resultat(I) := 1;
else
Resultat(I) := 0;
end if;

But you will find other programmers who prefer the second form.

Steve
(The Duck)
Post by c***@mac.com
Hello all, I'm new to this and comming from Java environment. I have
this code to convert bytes to bits but I dont know what to put in the
last line of code to get on the screen my converted number. Thanks for
any help
----------------------------------------------------------
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_text_Io; use Ada.Integer_text_Io;
procedure Pas3 is
subtype Octet is Integer range 0..255;
subtype Bit is Integer range 0..1;
type Tab_Bits is array (0 .. 7) of Bit;
function Octet_Vers_Bits (Oc : Octet )
return Tab_Bits is
Resultat : Tab_Bits;
N : Octet;
begin
N := Oc;
for I in reverse 0..7 loop
if ( N mod 2 = 1 ) then
Resultat(I) := 1;
else
Resultat(I) := 0;
end if;
N := N / 2;
end loop;
return Resultat;
end Octet_Vers_Bits;
t1 : Tab_bits;
x : integer;
begin
Put (" donner x: ");
Get(x);
for I in 0 .. 7 loop
t1 := Octet_Vers_Bits(x);
Put(Bit(x)); ----------------> here is the problem, what should I
put here.
end loop;
end Pas3 ;
Loading...