简体   繁体   English

C将Char数组(字符串)转换为int数组

[英]C Converting Char array (strings) into int array

I have been searching around for hours for this, but I can't seem to find an answer, especially when it comes to C language. 我一直在寻找几个小时,但我似乎无法找到答案,特别是在C语言方面。 In java it would be simple matter of casting and such, but I just cant seem to get a firm grip on it in C. 在java中,这将是一个简单的铸造等问题,但我似乎无法在C中牢牢抓住它。

My problem is: I have to get an input from a user, they can enter any number from 0 to 31. They can enter up to 31 numbers. 我的问题是:我必须从用户那里获得输入,他们可以输入0到31之间的任何数字。他们最多可以输入31个数字。 They can repeat. 他们可以重复。 The input could look like this: 3 15 32. I know how to take this input and store it as a string using the fgets() function. 输入可能如下所示:3 15 32.我知道如何使用fgets()函数获取此输入并将其存储为字符串。 I store the input in array s[]; 我将输入存储在数组s []中;

Now, the part that I am stuck on, how do I convert that to an int array so that int int[0]=3, i[1]=15, i[2]=32, etc. 现在,我坚持的部分,如何将其转换为int数组,以便int int [0] = 3,i [1] = 15,i [2] = 32,等等。

I tried using sscanf(s, "%d", int), but it only is able to get the first number from the input, stores it in int[0], and then doesn't fill in the rest. 我尝试使用sscanf(s,“%d”,int),但它只能从输入中获取第一个数字,将其存储在int [0]中,然后不填写其余部分。 Is there some kind of a function that makes this all easy and quick? 是否有某种功能使这一切变得简单快捷?

Thank you in advance. 先感谢您。

You can use a library or you can write a helper function. 您可以使用库,也可以编写辅助函数。 The helper function would look something like this (pseudocode): 辅助函数看起来像这样(伪代码):

int output = 0;
for (int i = 0; i < string.length; i++) {
  output = output * 10;
  output = output + parse(string[i]) //parse first character into a single digit
}

For example, if you have 253, it will read 2. Then it will multiply it by 10 (20) and add 5 (25). 例如,如果你有253,它将读为2.然后它将乘以10(20)并加5(25)。 Then it will multiply that by 10 (250) and add 3, which will be the expected output. 然后它将乘以10(250)并加3,这将是预期的输出。

Note that you need to find a way to parse a single character of your string into a single digit integer - that should be very easy using something like a switch() statement. 请注意,您需要找到一种方法将字符串的单个字符解析为单个数字整数 - 使用类似switch()语句的方法应该非常简单。

EDIT: If you are looking for a library function, check out this question: 编辑:如果您正在寻找库函数,请查看此问题:

What is the difference between sscanf or atoi to convert a string to an integer? 将字符串转换为整数的sscanf或atoi有什么区别?

You might be overthinking things. 你可能会过度思考问题。 Here's a simply way to input numbers from a user that doesn't involve formatting or parsing strings. 这是从用户输入不涉及格式化或解析字符串的数字的简单方法。

int numArray[31];
int i = 0;
int num = 0;

while( scanf("%d", &num) > 0 && i < 31 && num >0) {
    numArray[i] = num;
    i++;
}

The loop will stop when the user enters a negative number, or has entered all 31 possible numbers. 当用户输入负数或已输入所有31个可能的数字时,循环将停止。

You still have to make sure the user enters a number between 0 and 31 though! 您仍然必须确保用户输入0到31之间的数字!

It's been a while since I've worked with 'C', but here's the basic idea. 我用'C'已经有一段时间了,但这是基本的想法。 To convert the char array values to integers, you'll basically have to assign each value to an int var such as: 要将char数组值转换为整数,您基本上必须将每个值分配给int var,例如:

i[0] = c[0]
i[1] = c[1]
i[2] = c[2]

The reason you can't cast the numbers directly to integer references is because C allots a contiguous memory space to hold those 3 characters. 你不能将数字直接转换为整数引用的原因是因为C分配一个连续的内存空间来容纳这3个字符。 Depending on the compiler you are using will determine the char size and int size. 根据您使用的编译器将确定char大小和int大小。 But if the chars are 8 bits each, then an array of 3 chars is 3 bytes. 但如果字符各为8位,则3个字符的数组为3个字节。 16 bit int would equal 6 bites. 16位int将等于6咬。 If you were to cast directly to ints, you wouldn't get the correct value translations because memory addresses would overlap. 如果您直接转换为整数,则无法获得正确的值转换,因为内存地址会重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM