String To Integer???

 

Hello, everyone!

Seems like I need help, I spent hours trying to figure out a simple matter (at least I thought it would be simple). So, I'm writing this function in my expert, that would take the symbol name (string) and convert it to a number (integer), so the function

StrToInteger()

seems like a good idea, but instead of making a string number an integer, it transforms the number.

To be more clear, here is the function:

int MagicNumberGen(string pair){

string magic;

int length = StringLen(pair);

for (int i=0;i<length;i++){

magic = StringConcatenate(magic, StringGetChar(pair, i));

}

int magic_int = StrToInteger(magic);

return (magic_int );

}

now, it does return a number, but not quite correct one. The problem seems to be when it gets converted into an integer. For instance, when I call this function on "xauusd", the string 'magic' inside the function says '886585858368', which is the code in ASCII for this string, however, when this number transforms to an integer, it returns '2147483647'

I tried even converting it to the double (works perfectly) and then to integer, but it gets even worse: '-2147483648'

Feel like I'm doing something wrong, but don't know where. I'd appreciate any suggestions.
 
Tarasics: the function says '886585858368', which is the code in ASCII for this string, however, when this number transforms to an integer, it returns '2147483647'
2147483647 (7FFFFFFF) is the largest integer possible (32 bit signed integer)
 

Your result is 12 digits long, the max. integer is 10 digits, it simply does not fit in integer type.

What about double, why not use double type? Oh, I know, magic nr should be integer.

I suggest consider using another method to calculate your unique magicnr. You have a double (or string) with max. 24 chars between 0..9.

BCD may be good to have this number half long: http://en.wikipedia.org/wiki/Binary-coded_decimal

 

Okey, now it makes sense. I always forget about the basics. I can simply cut my string number to 9 symbols and it should be fine.

Thanks a lot, WHRoeder and szgy74.