如何从一个字符串得到两个号码,转换一个变量为int? [英] How to get two number from a string and convert to int in one variable?
问题描述
我试图完成了我的功课,这是我无法弄清楚,因为今天下午的最后一件事。
比方说我有串这样的数组 ABCD24EFG
中,我想这个数字 24
中我初始化哪种类型的变量 INT
我可以用一个单一号码做到这一点,并将其转换像这样
number_holder = ARRAY_NAME [4] - '0';
和我会得到 2
在 number_holder
,但我怎么能得到整个像 24
,并将其转换成 INT
键入<?/ p>
您可以使用此逻辑。
number_holder = ARRAY_NAME [4] - '0';
number_holder = number_holder * 10 +(ARRAY_NAME [5] - '0');
此方式,您也可以处理像ABCD243EFG,ABCD2433EFG ...
数组值对于整数0-9 ASCII值48 - 57 ..所以用这个在阵列发现的整数
number_holder = 0;
对于(INT I = 0; I&LT; arraylength;我++)
{
如果(阵列[1] - ; 58安培;&安培;阵列[1] - GT; 47)
number_holder = number_holder * 10 +阵列[I] - '0';
}
您将有你的结果number_holder。
I'm trying to finish up my homework and this is the last thing I couldn't figure it out since this afternoon.
Say for example I have string in the array like this ABCD24EFG
and I want to get that number 24
in a variable that I initialize which type is int
I can do it with one single number and convert it like this
number_holder = array_name[4] - '0';
and I will get 2
in the number_holder
but how can I get the whole like 24
and convert them into int
type?
You can use this logic.
number_holder=array_name[4] - '0';
number_holder=number_holder*10 + (array_name[5] - '0');
This way you can also handle array values like ABCD243EFG, ABCD2433EFG ...
ASCII value for integers 0-9 are 48 - 57 ..So use this for finding integers in array.
number_holder=0;
For (int i=0;i<arraylength;i++)
{
if(array[i]<58 && array[i]>47)
number_holder=number_holder*10+array[i] - '0';
}
You will have your result in number_holder.
这篇关于如何从一个字符串得到两个号码,转换一个变量为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!