Im having a bit of trouble trying to store an ID number, or even a phone number as an Integer value.

This is what ive tried:

begin
  InfoFile := TStringList.Create;


    ID := StrToInt(edtID.Text);
    PhoneNumber := StrToInt(edtPhone.Text);

I keep on getting ***** is not a valid integer value. First of all, I know the number is too big for an integer value, is there a way to compress or make it smaller somehow? Or should I just not use it as a variable? I dont really know if im making too much sense.

Any advice would be appreciated!

有帮助吗?

解决方案

It should be obvious that you cannot compress a phone number. Compression has to be reversible. If you could compress the number so that, for instance, you could store, say, a 40 bit value in 32 bits, then your compression would not be reversible. There are 232 distinct values that can be stored in a 32 bit value. If there are more than 232 distinct telephone numbers, then compression cannot possibly help.

Probably the most expedient solution, if you must store as an integer, is to store the number as a 64 bit integer, Int64. Convert from string using StrToInt64. The largest value that can be stored in a 64 bit integer is 9,223,372,036,854,775,807.

You will clearly need to sanitise your string before you attempt to call StrToInt64. You'll need to remove spaces, hyphens etc. And are you sure that you can store the number as an integer. For instance, I might write a UK telephone number as +44 151 876 2765. What will you do with the +?

So, whilst Int64 gives you more range, you might wish to consider whether or not the value would be better stored in a string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top