* Re: Base 12 Integer IO
[not found] <mailman.12.1044286941.3911.comp.lang.ada@ada.eu.org>
@ 2003-02-03 16:02 ` Mark Biggar
2003-02-03 16:42 ` Martin Dowie
` (3 subsequent siblings)
4 siblings, 0 replies; 5+ messages in thread
From: Mark Biggar @ 2003-02-03 16:02 UTC (permalink / raw)
Paolo Argenton wrote:
> Hi all !
>
> I would like to do base 12 Integer IO;
>
> while the following code fragment works
>
> for I in 0..1488 loop
>
> Put( I, Width => 3, Base => 12 );
>
> It has 2 drawbacks (for me at least)
>
> 1) the output is with leading base number and # i.e. 12#A40# while I
> would like to have the raw A40, for instance.
>
> 2) width is not fixed, i.e. I would like to have in the output 005 for
> -say- number 5 instead of one digit only.
>
> How can I fix these things? Can I do Integer IO to a string, in order to
> reformat the string as I desire?
Yes, this allows you to do the usual trick to get leading zero's.
tmp: string(1..8);
for I in 0..12#bbb# loop
put(tmp, 12#1000# + I, base => 12);
put(tmp(5..7));
end loop;
--
Mark Biggar
mark.a.biggar@attbi.com
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Base 12 Integer IO
[not found] <mailman.12.1044286941.3911.comp.lang.ada@ada.eu.org>
2003-02-03 16:02 ` Base 12 Integer IO Mark Biggar
@ 2003-02-03 16:42 ` Martin Dowie
2003-02-03 18:56 ` Georg Bauhaus
` (2 subsequent siblings)
4 siblings, 0 replies; 5+ messages in thread
From: Martin Dowie @ 2003-02-03 16:42 UTC (permalink / raw)
> I would like to do base 12 Integer IO;
>
> while the following code fragment works
>
> for I in 0..1488 loop
>
> Put( I, Width => 3, Base => 12 );
[snip]
>
> How can I fix these things? Can I do Integer IO to a string, in order to
reformat the string as I desire?
You don't use Integer IO to reformat the the string but I would use
Ada.Integer_Text_IO
to convert to the 12#...# format string you have, then use the 'Index'
routine in Ada.Strings.Fixed
to find the first hash (and thus the first numerical value). Then string
slice the 1, 2 or n base12 digits
into a fixed sized, zero filled string (e.g. "000").
Then just call Ada.Text_IO.Put with this value.
Shouldn't take more than about 6 statements. :-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Base 12 Integer IO
[not found] <mailman.12.1044286941.3911.comp.lang.ada@ada.eu.org>
2003-02-03 16:02 ` Base 12 Integer IO Mark Biggar
2003-02-03 16:42 ` Martin Dowie
@ 2003-02-03 18:56 ` Georg Bauhaus
2003-02-04 3:37 ` Steve
2003-02-04 10:58 ` Dmitry A. Kazakov
4 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2003-02-03 18:56 UTC (permalink / raw)
Paolo Argenton <paoloa1@yahoo.com> wrote:
:
: I would like to do base 12 Integer IO;
The following certainly has some flaws, but it might, I hope,
provide something with which you can start.
The package `dozens' allows a conversion to plain base 12
number strings, 0-padding is added in a test routine that follows:
with Ada.Text_IO; use Ada.TExt_IO;
with Ada.strings.fixed; use Ada.strings.fixed;
with dozens; use dozens;
procedure dtest is
max_digits: constant := 4;
-- duodecimal digits needed, adjust accordingly, affects padding
function add_padding(nrep: String) return String;
-- adds padding to number string `nrep`
function add_padding(nrep: String) return String is
offset: constant Integer := Boolean'pos(nrep(nrep'first) = '-');
-- nrep represents a negative number iff 1
begin
return (offset * '-') &
tail(nrep(nrep'first + offset.. nrep'last), max_digits, pad => '0');
end add_padding;
begin
for d in Base_12_int(-12)..12 loop
put_line(add_padding(dozens.to_string(d)) &
" for decimal " & Base_12_int'image(d));
end loop;
end dtest;
package dozens is
type Base_12_int is range -(12**10).. 12**10 - 1;
function to_string(n: Base_12_int) return String;
end dozens;
with Ada.text_IO;
with Ada.strings.fixed;
use Ada;
package body dozens is
package B12_IO is new Text_IO.Integer_IO(num => Base_12_int);
-- see body statements below for default settings
---------------
-- to_string --
---------------
-- we know the prefix "-12#" has a length of 4 characters,
-- and there is "#" in the end
function to_string (n: Base_12_int) return String is
use B12_IO, strings.fixed;
result: string (1..default_width + 5); -- enough given a base >= 10
begin
put(result, n);
declare
bare: constant String := trim(result, strings.left);
begin
if n < 0 then
return '-' & bare(5.. bare'last-1);
else
return bare(4.. bare'last-1);
end if;
end;
end to_string;
begin
B12_IO.default_base := 12;
end dozens;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Base 12 Integer IO
[not found] <mailman.12.1044286941.3911.comp.lang.ada@ada.eu.org>
` (2 preceding siblings ...)
2003-02-03 18:56 ` Georg Bauhaus
@ 2003-02-04 3:37 ` Steve
2003-02-04 10:58 ` Dmitry A. Kazakov
4 siblings, 0 replies; 5+ messages in thread
From: Steve @ 2003-02-04 3:37 UTC (permalink / raw)
"Paolo Argenton" <paoloa1@yahoo.com> wrote in message
news:mailman.12.1044286941.3911.comp.lang.ada@ada.eu.org...
Hi all !
I would like to do base 12 Integer IO;
while the following code fragment works
for I in 0..1488 loop
Put( I, Width => 3, Base => 12 );
It has 2 drawbacks (for me at least)
1) the output is with leading base number and # i.e. 12#A40# while I would
like to have the raw A40, for instance.
2) width is not fixed, i.e. I would like to have in the output 005 for -say-
number 5 instead of one digit only.
How can I fix these things? Can I do Integer IO to a string, in order to
reformat the string as I desire?
Thanks everybody
Paolo
Here is a small example (that runs and produces the correct result):
WITH Ada.Strings.Fixed;
USE Ada.Strings.Fixed;
WITH Ada.Strings.Maps;
USE Ada.Strings.Maps;
WITH Ada.Text_Io;
USE Ada.Text_Io;
WITH Ada.Integer_Text_Io;
USE Ada.Integer_Text_Io;
PROCEDURE GetNumString IS
value : Integer := 42;
buffer : String( 1 .. 32 );
BEGIN
Put( buffer, value, 12 );
DECLARE
lbSet : Character_Set := To_Set( '#' );
notLb : Character_Set := NOT lbSet;
text : String := Tail( Trim( Trim( buffer, notLb, notLb ),
lbSet, lbSet), 4, '0' );
BEGIN
Put_Line( text );
END;
END GetNumString;
Yahoo! Cellulari: loghi, suonerie, picture message per il tuo telefonino
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Base 12 Integer IO
[not found] <mailman.12.1044286941.3911.comp.lang.ada@ada.eu.org>
` (3 preceding siblings ...)
2003-02-04 3:37 ` Steve
@ 2003-02-04 10:58 ` Dmitry A. Kazakov
4 siblings, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2003-02-04 10:58 UTC (permalink / raw)
On Mon, 3 Feb 2003 16:42:11 +0100 (CET), Paolo Argenton
<paoloa1@yahoo.com> wrote:
>I would like to do base 12 Integer IO;
>
>while the following code fragment works
>
> for I in 0..1488 loop
>
> Put( I, Width => 3, Base => 12 );
>
>
>It has 2 drawbacks (for me at least)
>
>1) the output is with leading base number and # i.e. 12#A40# while I would like to have the raw A40, for instance.
>
>2) width is not fixed, i.e. I would like to have in the output 005 for -say- number 5 instead of one digit only.
>
>How can I fix these things? Can I do Integer IO to a string, in order to reformat the string as I desire?
You can use http://www.dmitry-kazakov.de/ada/strings_edit.htm. It has
properties 1, 2.
---
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 5+ messages in thread