From: Warren on
Ludovic Brenta expounded in
news:2f4313a5-bb3d-4f7f-8a86-7c8f7d549c53(a)k41g2000yqf.googlegroups.com:

...
> You could use the predefined library to get a result using the Ada
> "syntax for based literal" (ARM A.10.8(14)), e.g. 16#CE#.
>
> package Stream_Element_IO is
> new Ada.Text_IO.Modular_IO (Num => Ada.Streams.Stream_Element);
>
> E : Ada.Streams.Stream_Element := ...
> begin
> Stream_Element_IO.Put (Item => E, Base => 16);
>
> (There are variants of Put that send the output to a File_Type or to a
> string)..
> Ludovic Brenta.

I've been interested in finding these ways to "Put" (Hex) to
a string, instead of a File_Type.

But what I've seen is that "Put" always involves a
File_Type (or implies one).

So what have I missed?

Warren
From: Jeffrey R. Carter on
On Apr 19, 9:24 am, Ludovic Brenta <ludo...(a)ludovic-brenta.org> wrote:
>
> If you want to avoid the 16## part, you can easily convert the
> stream_elements to a string representation yourself like so:
>
> function To_Hex (E : in Ada.Streams.Stream_Element) return String is

This seems unnecessarily complex to me. Using the standard library to
put an element to a String in base 16, then stripping the "16#" from
the front and '#' from the end seems simpler, clearer, and less error
prone.

The PragmAda Reusable Components include Image functions with optional
Width, Zero_Filled, and Base parameters that do not include any base
indicator in the image. See PragmARC.Images and PragmARC.Images.Image.

http://pragmada.x10hosting.com/
From: Jeffrey R. Carter on
On Apr 19, 11:05 am, Warren <ve3...(a)gmail.com> wrote:
>
> I've been interested in finding these ways to "Put" (Hex) to
> a string, instead of a File_Type.
>
> But what I've seen is that "Put" always involves a
> File_Type (or implies one).
>
> So what have I missed?  

From A.10.8 (Input-Output for Integer Types):

15
procedure Get(From : in String; Item : out Num; Last : out Positive);
16
Reads an integer value from the beginning of the given string,
following the same rules as the Get procedure that reads an integer
value from a file, but treating the end of the string as a file
terminator. Returns, in the parameter Item, the value of type Num that
corresponds to the sequence input. Returns in Last the index value
such that From(Last) is the last character read.
17
The exception Data_Error is propagated if the sequence input does not
have the required syntax or if the value obtained is not of the
subtype Num.
18
procedure Put(To : out String;
Item : in Num;
Base : in Number_Base := Default_Base);
19
Outputs the value of the parameter Item to the given string, following
the same rule as for output to a file, using the length of the given
string as the value for Width.
From: Warren on
Jeffrey R. Carter expounded in news:e5748cb1-0e95-49ff-8616-
6709c6dd7c1f(a)q15g2000yqj.googlegroups.com:

> On Apr 19, 11:05�am, Warren <ve3...(a)gmail.com> wrote:
>>
>> I've been interested in finding these ways to "Put" (Hex) to
>> a string, instead of a File_Type.
>>
>> But what I've seen is that "Put" always involves a
>> File_Type (or implies one).
>>
>> So what have I missed? �
>
> From A.10.8 (Input-Output for Integer Types):
....
> procedure Put(To : out String;
> Item : in Num;
> Base : in Number_Base := Default_Base);
> 19
> Outputs the value of the parameter Item to the given string, following
> the same rule as for output to a file, using the length of the given
> string as the value for Width.

My apologies-- I see it now. I hate these dos names
that gnat uses for the spec file names. I see the
api now.

Now if only I could find where the mayonnaise in the
fridge is..

Warren
From: Ludovic Brenta on
tonyg writes on comp.lang.ada:
> Changed the hex function to
> function To_Hex (E : in Ada.Streams.Stream_Element) return String
> is
> -- Warning: not compiled and not tested...
> X : constant array (0 .. 15) of Character :=

(1) I should have written:

X : constant array (Ada.Streams.Stream_Element range 0 .. 15) of Character :=

> ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
> 'C', 'D', 'E', 'F');
> Result : String (1 .. Ada.Streams.Stream_Element'Size / 4); -- 1 hex digits = 4 bits
> Working_Copy : Ada.Streams.Stream_Element := E;
> use type Ada.Streams.Stream_Element;
> First_Character : Natural := 0;
> Base : constant := 16;
> begin
> for K in reverse Result'First .. Result'Length loop

(2) and this should be:

for K in reverse Result'Range loop

> Result (K) := X (integer(Working_Copy) mod integer (Base) );

(3) and thanks to (1), this can come back to the simpler:

Result (K) := X (Working_Copy mod Base);

> Working_Copy := Working_Copy / Base;
> if Working_Copy = 0 then
> First_Character := K;
> exit;
> end if;
> end loop;
> return Result (First_Character .. Result'Last);
> end To_Hex;
>
> It still seems to be dropping a few zeros though and I'm stumped where
> its going wrong

I tested it with 42 and 0 and correctly got 2A and 0, so I don't know
what you mean by that.

Maybe the fact that the result has a variable width is a problem? If
so, here is a fixed-width variant which is actually a bit simpler:

function To_Hex (E : in Ada.Streams.Stream_Element) return String is
X : constant array (Ada.Streams.Stream_Element range 0 .. 15)
of Character :=
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F');
Result : String
(1 .. Ada.Streams.Stream_Element'Size / 4) -- 1 hex digits = 4 bits
:= (others => '0');
Working_Copy : Ada.Streams.Stream_Element := E;
use type Ada.Streams.Stream_Element;
Base : constant := 16;
begin
for K in reverse Result'Range loop
Result (K) := X (Working_Copy mod Base);
Working_Copy := Working_Copy / Base;
end loop;
return Result;
end To_Hex;

and this yields 2A and 00 for my "test vector".

--
Ludovic Brenta.