String 将字符串中的int追加到int[]

String 将字符串中的int追加到int[],string,winforms,visual-c++,int,String,Winforms,Visual C++,Int,假设有一个字符串“123124125”。 我希望从字符串中取出每3个字符并存储到整数数组中 例如 下面让字符串密文为“123124125”: String^密文; int length1=密文->长度; 整数计数=0; int count1=0; 同时(计数长度; 整数计数=0; int count1=0; 同时(计数

假设有一个字符串“123124125”。 我希望从字符串中取出每3个字符并存储到整数数组中

例如

下面让字符串密文为“123124125”:

String^密文;
int length1=密文->长度;
整数计数=0;
int count1=0;
同时(计数<长度1)
{
数字[count1]=(密文[count]*100)+(密文[count+1]*10)+密文[count+2]);
计数=计数+3;
count1++;
}
上面是我写的代码。结果应该是编号[]内的123,但不是

密文[count]
乘以100时,它不使用“1”来乘以100,而是它的十进制数。因此,十进制中的“1”是“50”,因此结果是“5000”,而不是100

我的问题是如何将它们3乘3追加到int[]中?我如何避免使用十进制而使用1直

对不起,我的英语不好。非常感谢您的帮助,提前谢谢。

编辑。我曾建议使用9-('9'-char),但正如gkovacs90在他的回答中所建议的,char-'0'是更好的书写方式

原因是
ciphertext[count]
是一个字符,因此将其转换为int将为该字符提供ascii码,而不是整数。您可以执行类似于
ciphertext[count])-“0”这样的操作

例如,假设
密文[count]为“1”
。字符
1
的ascii值为49(请参阅)。因此,如果你做
密文[count]*100
会给你4900

但是如果你做了
ciphertext[count]-“0”
你会得到49-48==1

所以

字符串密文;
int length1=密文->长度;
整数计数=0;
int count1=0;
同时(计数<长度1)
{
编号[计数1]=
((密文[计数]-'0')*100)+
((密文[计数+1]-“0”)*10+
(密文[计数+2]-“0”);
计数=计数+3;
count1++;
}

我将使用
密文[count]-“0”
获取字符的int值


您还可以对要转换为整数的子字符串使用atoi函数。

其他人指出了您的错误。另外,这样做怎么样

string str = "123124125"; 

int i = str.Length / 3;

int[] number = new int[i];

while(--i>=0) number[i] = int.Parse(str.Substring(i*3,3));

谢谢大家!!这是工作!感谢gkovacs90为我提供建议,而Jimbo为我提供了解释,现在我完全理解了。。谢谢你,我建议了另一种方法……)
String ^ ciphertext;
int length1 = ciphertext-> Length;
int count = 0;
int count1 = 0;

while (count < length1)
{
    number[count1] = (ciphertext[count] * 100) + (ciphertext[count+1] * 10) + ciphertext[count+2]);
    count = count + 3;
    count1++;
}
String ciphertext;
int length1 = ciphertext-> Length;
int count = 0;
int count1 = 0;

while (count < length1)
{
    number[count1] = 
        ((ciphertext[count] -'0') * 100) + 
        ((ciphertext[count+1] - '0') * 10) + 
        (ciphertext[count+2] - '0');
    count = count + 3;
    count1++;
}
string str = "123124125"; 

int i = str.Length / 3;

int[] number = new int[i];

while(--i>=0) number[i] = int.Parse(str.Substring(i*3,3));