sYou are given a integer in string format;
String sNum = "25";
int iNum = StrToInt(sNum);
How does StrToInt() work?
Crudely it's as follows.
You go digit by digit. Each digit has a hex value.
0 0x30 48
1 0x31 49
2 0x32 50
3 0x33 51
4 0x34 52
....
9 0x39 57
The algorithm pseduo-code is as follows.
1. result = 0;
2. for each character i in sNum
3. result = result*10;
4. result = result + ASCIIVal(sNum[i]) - ASCIIVal('0');
Let's take 25 for example.
For the first iteration;
sNum[0] = 2;
result = 0*10=0;
result = 0 + (50-48) = 2;
For the second iteration;
sNum[1] = 5;
result=2*10=20;
result=20+(53-48)=25
String sNum = "25";
int iNum = StrToInt(sNum);
How does StrToInt() work?
Crudely it's as follows.
You go digit by digit. Each digit has a hex value.
0 0x30 48
1 0x31 49
2 0x32 50
3 0x33 51
4 0x34 52
....
9 0x39 57
The algorithm pseduo-code is as follows.
1. result = 0;
2. for each character i in sNum
3. result = result*10;
4. result = result + ASCIIVal(sNum[i]) - ASCIIVal('0');
Let's take 25 for example.
For the first iteration;
sNum[0] = 2;
result = 0*10=0;
result = 0 + (50-48) = 2;
For the second iteration;
sNum[1] = 5;
result=2*10=20;
result=20+(53-48)=25
Comments
Post a Comment