Converting string to float and integer 
Author Message
 Converting string to float and integer

Hi,

I am tring to convert a string into a float and/or integer.

Ihave tried integer(string), but I get illegal operand for numeric
conversion.

eg code.
answer := Integer(string(1..2));

I am using Gnat Ada (95)

Any help would be greatful.
thanks

Dan.



Thu, 27 Jun 2002 03:00:00 GMT  
 Converting string to float and integer

Quote:

> I am tring to convert a string into a float and/or integer.

Every scalar type comes with predefined attributes to convert from
string to that type, and from that type to a string.

Convert_String_To_Int:
declare
  Image : constant String := "42";
  Value : constant Integer := Integer'Value (Image);
begin
  null;
end Convert_String_To_Int;

Convert_Int_To_String:
declare
  Value : constant Integer := 42;
  Image : constant String := Integer'Image (Value);
begin
  null;
end Convert_Int_To_String;

To convert between Float and String, use Float'Image and Float'Value.

To convert between any scalar type T and String, use T'Image and
T'Value.

--
Help keep evolution in the science classroom and religion out: become a
member of the National Center for Science Education.

<http://www.natcenscied.org/>



Thu, 27 Jun 2002 03:00:00 GMT  
 Converting string to float and integer


Quote:
>Hi,

>I am tring to convert a string into a float and/or integer.

Here is a little program I sometimes use in Ada classes to
demonstrate the options for satisfying this requirement.

-- ========== String_To_Scalar_Demonstration ===========
-- String_To_Scalar_Demonstration.adb  by Richard Riehle
-- This program demonstrates several ways to convert a
-- a string to a scalar value.
--
-- =====================================================
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
use  Ada;
procedure String_To_Scalar_Demonstration is
  type Spectrum is (Red, Orange, Yellow, Green,
                    Blue, Indigo, Violet);
  type Unsigned is mod 2**8;
  Num   : Integer  := 0;
  FNum  : Float    := 0.0;
  Color : Spectrum := Blue;
  MNum  : Unsigned := 0;
  Text  : String(1..10);
  Text_Integer  : String := "451";
  Text_Float    : String := "360.0";
  Text_Color    : String := "Orange";
  Text_Unsigned : String := "42";
  Integer_Last  : Natural;
  Float_Last    : Natural;
  Spectrum_Last : Natural;
  Modular_Last  : Natural;
  package SIO is new Text_IO.Enumeration_IO(Enum => Spectrum);
  package MIO is new Text_IO.Modular_IO    (Num  => Unsigned);
  package IIO is new Text_IO.Integer_IO    (Num  => Integer);
  package FIO is new Text_IO.Float_IO      (Num  => Float);
begin
  Text_IO.Put_Line("The String Values are: ");
  Text_IO.Put("Orange for Enumerated Type          ");
  Text_IO.Put_Line("451  for Integer Type  ");
  Text_IO.Put("360.0  for Float Type               ");
  Text_IO.Put_Line("42  for Unsigned Type  ");
  Text_IO.New_Line;
  -- Example 1; using the Value attribute
  Text_IO.New_Line;
  Text_IO.Put_Line("   >>>> Example 1; Using 'Value Attribute <<<< ");
  Color := Spectrum'Value(Text_Color);
  Num   := Integer'Value(Text_Integer);
  FNum  := Float'Value(Text_Float);
  MNum  := Unsigned'Value(Text_Unsigned);
  SIO.Put(Color);   Text_IO.New_Line;
  IIO.Put(Num);     Text_IO.New_Line;
  FIO.Put(Fnum);    Text_IO.New_Line;
  MIO.Put(MNum);    Text_IO.New_Line;
  Text_IO.New_Line;
  -- Example 2; using the procedures of pre-instantiated packages
  Text_IO.Put_Line("   >>>> Example 2; using pre-instantiated packages
<<<< " );
  Integer_Text_IO.Get(From => Text_Integer,
                      Item => Num,
                      Last => Integer_Last);
  Float_Text_IO.Get(From => Text_Float,
                    Item => FNum,
                    Last => Float_Last);
  Integer_Text_IO.Put(Num); Text_IO.New_Line;
  Float_Text_IO.Put  (FNum, Fore => 3, Aft => 3, Exp => 0);  
                            Text_IO.New_Line(2);
  -- Example 3; using your own instantiated packages
  Text_IO.Put_Line("   >>>> Example 3; Using own instantiations <<<< ");
  Text_IO.New_Line;
  SIO.Get(From => Text_Color, Item => Color, Last => Spectrum_Last);  
  MIO.Get(From => Text_Unsigned, Item => MNum, Last => Modular_Last);
  IIO.Get(From => Text_Integer, Item => Num, Last => Integer_Last);    
  FIO.Get(From => Text_Float, Item => FNum, Last => Float_Last);
  -- Now Write the Results to the Screen
  SIO.Put(Item => Color);  Text_IO.New_Line;
  IIO.Put(Item => Num);    Text_IO.New_Line;
  FIO.Put(Item => FNum, Fore => 3, Aft => 3, Exp => 0);
  Text_IO.New_Line;
  MIO.Put(Item => MNum);  
  Text_IO.New_Line(2);
  Text_IO.Put_Line(" **** End of String_To_Scalar_Demonstration **** ");
end String_To_Scalar_Demonstration;

Hope this is helpful. You may copy it freely and modify it in any
way you wish.

Richard Riehle
http://www.adaworks.com



Thu, 27 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. converting strings to floats/integers

2. Converting Strings to Floats and/or Integers

3. Convert Strings Into Numbers (integer, float)

4. convert integer string to date string

5. Converting integer to float

6. Q: ways to convert floats to integers (fixnums)

7. strings to floats/integers

8. Converting a number in a String character to an Integer

9. Convert 2D-Integer-Array to String

10. How do I convert integer to string array?

11. How can I convert string to integer?

12. Convert STRING to INTEGER

 

 
Powered by phpBB® Forum Software