comp.lang.ada
 help / color / mirror / Atom feed
* convert integer base10 into base16
@ 2002-02-12 12:05 c-kif-kif
  2002-02-12 12:10 ` Lutz Donnerhacke
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: c-kif-kif @ 2002-02-12 12:05 UTC (permalink / raw)


I'm shure this question has been answered a million times, 
but i still can't get it right.

It's easy to convert base10 into base16 :
	a : integer := 16#A#;
	a := Integer'Value (Integer'Image(a)); -- result 10

But how can i convert from base10 to base16 or other ?

I don't want to write it on screen, so i think it's not with put/get how it works.

Thanks for any help
c-kif-kif



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

* Re: convert integer base10 into base16
  2002-02-12 12:05 convert integer base10 into base16 c-kif-kif
@ 2002-02-12 12:10 ` Lutz Donnerhacke
  2002-02-12 13:53 ` Alfred Hilscher
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Lutz Donnerhacke @ 2002-02-12 12:10 UTC (permalink / raw)


* c-kif-kif wrote:
>It's easy to convert base10 into base16 :
>	a : integer := 16#A#;
>	a := Integer'Value (Integer'Image(a)); -- result 10
>
>But how can i convert from base10 to base16 or other ?

I do not understand your problem:
  a : integer := 16#A#;     -- a is now 10
  a := Integer'Value (      -- this returns 10
         Integer'Image (a)  -- this returns " 10"
       );
Please specify your problem again.

Are you looking for a function taking a STRING and returning an INTEGER
assuming the string is hex?

  Integer'Value (Integer'Image(base) & '#' & input_string & '#')



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

* Re: convert integer base10 into base16
  2002-02-12 12:05 convert integer base10 into base16 c-kif-kif
  2002-02-12 12:10 ` Lutz Donnerhacke
@ 2002-02-12 13:53 ` Alfred Hilscher
  2002-02-12 14:33 ` Georg Bauhaus
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Alfred Hilscher @ 2002-02-12 13:53 UTC (permalink / raw)




c-kif-kif wrote:
> 
> I'm shure this question has been answered a million times,
> but i still can't get it right.
> 
> It's easy to convert base10 into base16 :
>         a : integer := 16#A#;
>         a := Integer'Value (Integer'Image(a)); -- result 10
> 
> But how can i convert from base10 to base16 or other ?
> 
> I don't want to write it on screen, so i think it's not with put/get how it works.
> 
> Thanks for any help
> c-kif-kif

You want convert a number to the hex-representation in a string? package
Integer_IO do this for you.

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.



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

* Re: convert integer base10 into base16
  2002-02-12 12:05 convert integer base10 into base16 c-kif-kif
  2002-02-12 12:10 ` Lutz Donnerhacke
  2002-02-12 13:53 ` Alfred Hilscher
@ 2002-02-12 14:33 ` Georg Bauhaus
  2002-02-12 16:45 ` Stephen Leake
  2002-02-13  6:17 ` Dale Stanbrough
  4 siblings, 0 replies; 7+ messages in thread
From: Georg Bauhaus @ 2002-02-12 14:33 UTC (permalink / raw)


c-kif-kif <c-kif-kif@eudoramail.com> wrote:
 
: It's easy to convert base10 into base16 :
: 
: I don't want to write it on screen, so i think it's not with
put/get how it works.
: 

Integer_IO also has
procedure Put(To   : out String;
              Item : in Num;
              Base : in Number_Base := Default_Base);
maybe this helps?

- georg



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

* Re: convert integer base10 into base16
  2002-02-12 12:05 convert integer base10 into base16 c-kif-kif
                   ` (2 preceding siblings ...)
  2002-02-12 14:33 ` Georg Bauhaus
@ 2002-02-12 16:45 ` Stephen Leake
  2002-02-13  6:17 ` Dale Stanbrough
  4 siblings, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2002-02-12 16:45 UTC (permalink / raw)


c-kif-kif@eudoramail.com (c-kif-kif) writes:

> I'm shure this question has been answered a million times, 
> but i still can't get it right.
> 
> It's easy to convert base10 into base16 :
> 	a : integer := 16#A#;
> 	a := Integer'Value (Integer'Image(a)); -- result 10
> 
> But how can i convert from base10 to base16 or other ?
> 
> I don't want to write it on screen, so i think it's not with put/get
> how it works.

It is _only_ when you write it to a screen or a string that the notion
of base 10 or base 16 matters. When an integer is stored in a
variable, such as 'a' above, it is represented as a particular pattern
of bits in RAM, normally base 2 binary.

Ada.Integer_Text_IO can write to a string as well as to the screen or
a file:

with Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Hex_Image is
   A : Integer := 10;
   Image_A : String (1 .. 10); -- bigger than needed, padded with spaces
begin
   Ada.Integer_Text_IO.Put (To => Image_A, Item => A, Base => 16);

   Ada.Text_IO.Put_Line ("The image we get is '" & Image_A & "'");
end Hex_Image;

-- 
-- Stephe



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

* Re: convert integer base10 into base16
  2002-02-12 12:05 convert integer base10 into base16 c-kif-kif
                   ` (3 preceding siblings ...)
  2002-02-12 16:45 ` Stephen Leake
@ 2002-02-13  6:17 ` Dale Stanbrough
  2002-02-13  9:44   ` c-kif-kif
  4 siblings, 1 reply; 7+ messages in thread
From: Dale Stanbrough @ 2002-02-13  6:17 UTC (permalink / raw)


 c-kif-kif@eudoramail.com (c-kif-kif) wrote:

> I'm shure this question has been answered a million times, 
> but i still can't get it right.
> 
> It's easy to convert base10 into base16 :
> 	a : integer := 16#A#;
> 	a := Integer'Value (Integer'Image(a)); -- result 10
> 
> But how can i convert from base10 to base16 or other ?
> 
> I don't want to write it on screen, so i think it's not with put/get how it 
> works.


If you are concerned about how it is stored inside the computer
you don't need to worry. The number 16#A# is the same number as 10,
and is stored in binary format inside the computer (it is converted
to a binary format by the compiler, or by the assembler). 

It is only when it is being displayed that it is changed from
binary to whatever base you want.

Dale



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

* Re: convert integer base10 into base16
  2002-02-13  6:17 ` Dale Stanbrough
@ 2002-02-13  9:44   ` c-kif-kif
  0 siblings, 0 replies; 7+ messages in thread
From: c-kif-kif @ 2002-02-13  9:44 UTC (permalink / raw)


Thanks for all your answers ! 

Ada.Integer_Text_IO.Put (To => Image_A, Item => A, Base => 16);

was just what i was looking for. And thanks again for the details
about how an integer is stored, independent of the reprensentation on
screen.

It works and i've learned a lot :-)

c-kif-kif



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

end of thread, other threads:[~2002-02-13  9:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-12 12:05 convert integer base10 into base16 c-kif-kif
2002-02-12 12:10 ` Lutz Donnerhacke
2002-02-12 13:53 ` Alfred Hilscher
2002-02-12 14:33 ` Georg Bauhaus
2002-02-12 16:45 ` Stephen Leake
2002-02-13  6:17 ` Dale Stanbrough
2002-02-13  9:44   ` c-kif-kif

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