do the calculation on a string data

Solved

Posted on 2006-04-25

Hi,

I need to store a 23 digit number in a 'numeric' variable. e.g. 20041010050500013402606

At the moment i have the string stored in a 'String' variable, However i need to convert it to a numeric type variable in order to apply a modulus 97.

I have tried with an integer variable using StrToInt(), I have tried with an Int64 variable using StrToInt64() but unfortunately it always come back with a compilation error "Integer constant too long". If i continue the execution of the program the the system throws an error message that reads "20041010050500013402606 is not a valid integer value"

I am currently using Delphi 6 enterprise edition.

I appreciate any help you can provide me with on this matter.

Cheers.

I need to store a 23 digit number in a 'numeric' variable. e.g. 20041010050500013402606

At the moment i have the string stored in a 'String' variable, However i need to convert it to a numeric type variable in order to apply a modulus 97.

I have tried with an integer variable using StrToInt(), I have tried with an Int64 variable using StrToInt64() but unfortunately it always come back with a compilation error "Integer constant too long". If i continue the execution of the program the the system throws an error message that reads "20041010050500013402606 is not a valid integer value"

I am currently using Delphi 6 enterprise edition.

I appreciate any help you can provide me with on this matter.

Cheers.

15 Comments

-9223372036854775808 .. 9223372036854775807

i.e. max of 19 digits, you need 23

Regards

Pierre

I need to apply a Modulus 97 to that value and as far as i understand the function MOD requires 2 integer input parameters so i do not know how to do that calculation with a string parameter, could you please instruct me how to do that?

Many thanks.

Thanks for the info. Do you have any ideas how can i work around this issue... maybe a user defined type or something like that.

Regards,

if it is greater or equal 97 subtract 97 from it

The difference multiply by 10 add next digit and continue.

http://delphiforfun.org/Pr

Operations supported are: Assign, Add, Subtract, Multiply, Divide, Modulo, Compare, Factorial, and ConvertToDecimalString.

It will support integers far larger than your needs

David

But is it worth to install those libraries for a few lines of code?

function Modulo97(s: string): integer;

var

val: integer;

i: integer;

begin

val := 0;

if Length(s) > 0 then

begin

val := Ord(s[1])-Ord('0');

i := 2;

while i <= Length(s) do

begin

val := 10*val+Ord(s[i])-Ord('0');

val := val mod 97;

Inc(i);

end;

end;

Result := val;

end;

I've assumed it's positive and I don't check for valid chars.

function Modulo97(s: string): integer;

var

val: integer;

i: integer;

begin

val := 0;

if Length(s) > 0 then

begin

i := 1;

while i <= Length(s) do

begin

val := 10*val+Ord(s[i])-Ord('0');

val := val mod 97;

Inc(i);

end;

end;

Result := val;

end;

function Modulo97(s: string): integer;

var

val: integer;

i: integer;

begin

val := 0;

for i := 1 to Length(s) do

begin

val := 10*val+Ord(s[i])-Ord('0');

val := val mod 97;

end;

Result := val;

end;

there is nothing to install for my code, for example (I didn't look at the others) It's just a copy paste in a unit and that's that :)

Title | # Comments | Views | Activity |
---|---|---|---|

How to add Windows metadata to a text file programmatically with Delphi? | 3 | 143 | |

Delphi XE10, DigitalPersona Fingerprint reader and MySQL | 6 | 126 | |

delphi exception | 7 | 55 | |

Delphi XE10, MySQL Query | 4 | 89 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**20** Experts available now in Live!