如何使用C将整数转换为字符数组

时间:2020-11-28 16:09:12

I want to convert integer number to character array in C.

我想在C中将整数转换为字符数组。

Input:

int num = 221234;

Result is equivalent to:

结果相当于:

char arr[6];
arr[0] = '2';
arr[1] = '2';
arr[2] = '1';
arr[3] = '2';
arr[4] = '3';
arr[5] = '4';

How can I do this?

我怎样才能做到这一点?

5 个解决方案

#1


15  

make use of log10 function to determine the number of digits & do like below

利用log10函数确定数字位数,如下所示

char * toArray(int number)
    {
        int n = log10(number) + 1;
        int i;
      char *numberArray = calloc(n, sizeof(char));
        for ( i = 0; i < n; ++i, number /= 10 )
        {
            numberArray[i] = number % 10;
        }
        return numberArray;
    }

or the other option is sprintf(yourCharArray,"%ld", intNumber);

或者另一个选项是sprintf(yourCharArray,“%ld”,intNumber);

#2


9  

'sprintf' will work fine, if your first argument is a pointer to a character (a pointer to a character is an array in 'c'), you'll have to make sure you have enough space for all the digits and a terminating '\0'. For example, If an integer uses 32 bits, it has up to 10 decimal digits. So your code should look like:

'sprintf'可以正常工作,如果你的第一个参数是一个指向字符的指针(指向一个字符的指针是'c'中的一个数组),你必须确保你有足够的空间容纳所有的数字并终止'\ 0'。例如,如果整数使用32位,则最多包含10位十进制数。所以你的代码应该是这样的:

int i;
char s[11]; 
...
sprintf(s,"%ld", i);

#3


1  

The easy way is by using sprintf. I know others have suggested itoa, but a) it isn't part of the standard library, and b) sprintf gives you formatting options that itoa doesn't.

简单的方法是使用sprintf。我知道其他人已经建议了itoa,但是a)它不是标准库的一部分,而b)sprintf为你提供了itoa没有的格式化选项。

#4


0  

You may give a shot at using itoa. Another alternative is to use sprintf.

你可以尝试使用itoa。另一种选择是使用sprintf。

#5


0  

Use itoa, as is shown here.

使用itoa,如此处所示。

char buf[5];
// convert 123 to string [buf]
itoa(123, buf, 10);

buf will be a string array as you documented. You might need to increase the size of the buffer.

你记录的时候buf将是一个字符串数组。您可能需要增加缓冲区的大小。

#1


15  

make use of log10 function to determine the number of digits & do like below

利用log10函数确定数字位数,如下所示

char * toArray(int number)
    {
        int n = log10(number) + 1;
        int i;
      char *numberArray = calloc(n, sizeof(char));
        for ( i = 0; i < n; ++i, number /= 10 )
        {
            numberArray[i] = number % 10;
        }
        return numberArray;
    }

or the other option is sprintf(yourCharArray,"%ld", intNumber);

或者另一个选项是sprintf(yourCharArray,“%ld”,intNumber);

#2


9  

'sprintf' will work fine, if your first argument is a pointer to a character (a pointer to a character is an array in 'c'), you'll have to make sure you have enough space for all the digits and a terminating '\0'. For example, If an integer uses 32 bits, it has up to 10 decimal digits. So your code should look like:

'sprintf'可以正常工作,如果你的第一个参数是一个指向字符的指针(指向一个字符的指针是'c'中的一个数组),你必须确保你有足够的空间容纳所有的数字并终止'\ 0'。例如,如果整数使用32位,则最多包含10位十进制数。所以你的代码应该是这样的:

int i;
char s[11]; 
...
sprintf(s,"%ld", i);

#3


1  

The easy way is by using sprintf. I know others have suggested itoa, but a) it isn't part of the standard library, and b) sprintf gives you formatting options that itoa doesn't.

简单的方法是使用sprintf。我知道其他人已经建议了itoa,但是a)它不是标准库的一部分,而b)sprintf为你提供了itoa没有的格式化选项。

#4


0  

You may give a shot at using itoa. Another alternative is to use sprintf.

你可以尝试使用itoa。另一种选择是使用sprintf。

#5


0  

Use itoa, as is shown here.

使用itoa,如此处所示。

char buf[5];
// convert 123 to string [buf]
itoa(123, buf, 10);

buf will be a string array as you documented. You might need to increase the size of the buffer.

你记录的时候buf将是一个字符串数组。您可能需要增加缓冲区的大小。