 |
|
|
|
|
|
|
 |
Converting decimal and hexdecimal number
Here is a function that convert a string to a decimal and hexdecimal in delphi as following.
function StrToInt(IntegerString:string):Integer;
Description
The StrToInt function converts an Integer string, IntegerString such as '123' into an Integer.
Example :
var
A, B, C, D, E : Integer;
begin
A := 456;
B := StrToInt('123');
C := StrToInt('-96');
D := StrToInt('$7A');
E := A + B + C + D;
ShowMessage('A : '+IntToStr(A));
ShowMessage('B : '+IntToStr(B));
ShowMessage('C : '+IntToStr(C));
ShowMessage('D : '+IntToStr(D));
ShowMessage('E : '+IntToStr(E));
end;
result :
A : 456
B : 123
C : -96
D : 122
E : 605
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.
|
|
|
|
|
|