Convert from String to Integer in Pascal

Asked

Viewed 521 times

0

Good evening, I have a problem in Pascal, I take a variable in String, the variable would be a Hexadecimal, I need to convert this String to integer and do not know how to do. Thank you

program trabalho_integrador ;
  
var
 a, b, c, a1, a2, b1, b2, c1, c2, calc2 : String;
 y : boolean;
 calc, i,z : integer;

begin
  write('Digite A: ');
  readln(a);
  write('Digite B: ');
  readln(b);
  write('Digite C: ');
  readln(c);


  a1 := copy(a,1,1);
  a2 := copy(a,2,1);
  b1 := copy(b,1,1);
  b2 := copy(b,2,1);
  c1 := copy(c,1,1);
  c2 := copy(c,2,1);

  z := StrToInt(a1);


  readln();

end.

2 answers

0

The Sysutils library has the converter as mentioned above, and also something I recommend that you check the need to use Strtointdef, following the example above, can be used as follows:

var
  A : Integer;
  B : Integer;


begin
  try
    A := StrToInt('100 ');    // Converte a String '100' para integer 100.
    B := StrToIntDef('Duzentos', 0); // Tenta converter o texto para inteiro, caso não consiga utiliza o valor passado no segundo parâmetro, que no caso é o 0.
  except
    on Exception : EConvertError do
      ShowMessage(Exception.Message);
  end;
end;

It is worth remembering that in the first case would enter the exception if it could not, in the second it would convert automatically without generating the exception.

0


There is the 'Strtoint' function in Pascal, as an example below:


var
  A : Integer;

Begin Try A := Strtoint('100 '); // Converts String '100' to integer 100. except on Exception : Econverterror do Showmessage(Exception.Message); end; end;

  • How do I import the library from this function?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.