Home     Projects     Micro     Tools     Delphi     Developer     Site map
HOME  
 
Delphi index
 


Converting decimal and hexdecimal number

Here is a function that convert a number to a string and in delphi as following.

function IntToStr(Value: Integer): string;

Description

IntToStr converts an integer into a string containing the decimal representation of that number.

Example :

var
   A, B, C, D : String;

begin
   A:=IntToStr(123);
   B:=IntToStr($123);
   C:=IntToStr(-123);
   D:=IntToStr(-$123);
   ShowMessage('A : '+A);
   ShowMessage('B : '+B);
   ShowMessage('C : '+C);
   ShowMessage('D : '+D);
end;

result :

   A : 123
   B : 291
   C : -123
   D : -291


Catching string to integer conversion errors

var
   A : Integer;

begin
   try
     A := StrToInt('123 '); 
   except
     on Exception : EConvertError do
       ShowMessage(Exception.Message);
   end;

   try
     A := StrToInt('$FG');
   except
     on Exception : EConvertError do
       ShowMessage(Exception.Message);
   end;
end;

result :

'123 ' is not a valid integer value
'$FG' is not a valid integer value

Trailing blanks after number 3 cuase error in the first error message and G cuase error in second error message becuase G is not a hex number.





 
Copyright(c) 2005-2008 sixca.com, All right reserved.
Best view @ 800X600, IE 6.0 up   
Terms of Use  Privacy Policy