Buscar..


Introducción

Los atributos de subtipo 'Image y 'Value tomarán, respectivamente, un valor escalar y una cadena y devolverán, respectivamente, una cadena y un valor escalar. El resultado de 'Image se puede ingresar a 'Value para obtener el valor original. Lo contrario también es cierto.

El __Scalar_Object__'Image se puede usar directamente en los objetos (desde Ada 2012-TC-1).

Sintaxis

  • function Scalar'Image (Argumento: Scalar'Base) return String;
  • function Discrete'Image (Argument: Discrete'Base) return String;
  • function Integer'Image (Argumento: Integer'Base) return String;
  • function Enumeration'Image (Argument: Enumeration'Base) return String;
  • function Real'Image (Argumento: Real'Base) return String;
  • function Numeric'Image (Argumento: Numeric'Base) return String;
  • función Scalar'Value (Argument: String) return Scalar'Base;
  • function Discrete'Value (Argument: String) return Discrete'Base;
  • función Integer'Value (Argumento: String) return Integer'Base;
  • function Enumeration'Value (Argument: String) return Enumeration'Base;
  • function Real'Value (Argument: String) return Real'Base;
  • function Scalar_Object 'Image return String;

Observaciones

Tenga en cuenta que 'Image puede incurrir en resultados definidos de implementación (RM 3.5), es decir, cuando algunos caracteres gráficos necesarios para el resultado de String no están definidos en Character . Considere los repertorios más grandes de 'Wide_Image y 'Wide_Wide_Image .

Ada 2012 (TC-1)

El permiso para usar el atributo __Scalar_Object__'Image directamente en un objeto se agregó en Ada 2012-TC-1 (abril de 2016).

Imprima el flotador usando el atributo Imagen

Ada 2012 (TC-1)
with Ada.Text_IO;

procedure Main is
   type Some_Float digits 8 range 0.0 .. 10.0;
   X : Some_Float := 2.71;
begin
   Ada.Text_IO.Put_Line (X'Image);
end Main;

Resultado

2.71000E+00

Imprima entero utilizando el atributo de imagen

Ada 2012 (TC-1)
with Ada.Text_IO;

procedure Main is
   type Some_Integer is range -42 .. 42;
   X : Some_Integer := 17;
begin
   Ada.Text_IO.Put_Line (X'Image);
end Main;

Resultado

17

Imprima la enumeración usando el atributo Imagen

Ada 2012 (TC-1)
with Ada.Text_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   X : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (X'Image);
   Ada.Text_IO.Put_Line (Pear'Image);
end Main;

Resultado

ORANGE
PEAR

Imprima la enumeración usando el atributo Imagen

with Ada.Text_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   X : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (Fruit'Image (X));
end Main;

Resultado

ORANGE

Imprimir entero con atributo de imagen

with Ada.Text_IO;

procedure Main is
   X : Integer := 17;
begin
   Ada.Text_IO.Put_Line (Integer'Image (X));
end Main;

Resultado

17

Imprime Float usando el atributo Imagen

with Ada.Text_IO;

procedure Main is
   X : Float := 2.71;
begin
   Ada.Text_IO.Put_Line (Float'Image (X));
end Main;

Resultado

2.71000E+00

Como lo inverso

with Ada.Text_IO;

procedure Image_And_Value is
   type Fruit is (Banana, Orange, Pear);
   X  : Fruit := Orange;
begin
   Ada.Text_IO.Put_Line (Boolean'Image
      (Fruit'Value (Fruit'Image (X)) = X
          and
       Fruit'Image (Fruit'Value ("ORANGE")) = "ORANGE"));
end Image_And_Value;

Resultado

TRUE


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow