C 将int转换为字符数组的函数,一些问题
此代码需要获取一个整数,并将其转换为表示该数字的字符数组: 例如: 号码是324 转换为: 比如:C 将int转换为字符数组的函数,一些问题,c,arrays,null,char,C,Arrays,Null,Char,此代码需要获取一个整数,并将其转换为表示该数字的字符数组: 例如: 号码是324 转换为: 比如: 我认为“\0”是空的,对吗 我的代码有什么问题 另外,我如何将其更改为一个函数,该函数获取一个整数并返回表示该整数的字符数组 #include <string.h> #include <stdlib.h> #include <stdio.h> void main () { int value = 324; char str [6]; cha
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void main ()
{
int value = 324;
char str [6];
char newstr [6];
int pointer, i = 0;
for(i=0; value>0; i++)
{
str[i] = value%10 + '0';
value /=10;
}
str[i] = '\0';
for(i=5; i>-1; i--)
{
if(str[i]!='\0')
{
newstr[pointer] = str[i];
pointer++;
}
}
for (i = 0; i < 6; i++)
{
printf("%d %c\n", i, newstr[i] );
}
printf ("shalom");
}
#包括
#包括
#包括
空干管()
{
int值=324;
char-str[6];
char newstr[6];
int指针,i=0;
对于(i=0;值>0;i++)
{
str[i]=值%10+'0';
数值/=10;
}
str[i]='\0';
对于(i=5;i>-1;i--)
{
如果(str[i]!='\0')
{
newstr[pointer]=str[i];
指针++;
}
}
对于(i=0;i<6;i++)
{
printf(“%d%c\n”,i,newstr[i]);
}
printf(“shalom”);
}
一个简单的方法应该是调用sprintf()
在您现有的代码中,
str
看起来像lile{'4','2','3','0',somethingdrandom,somethingdrandom}
。将其反转并分配给newstr
将使其成为{something random,something random',\0',3',2',4'}
,这绝对不是您想要的。实际上,当str[i]='\0'
时,您不会分配newstr
,这意味着newstr[2]==somethingdrandom
一个简单的方法应该是调用sprintf()
在您现有的代码中,str
看起来像lile{'4','2','3','0',somethingdrandom,somethingdrandom}
。将其反转并分配给newstr
将使其成为{something random,something random',\0',3',2',4'}
,这绝对不是您想要的。实际上,当str[i]='\0'
时,您不会分配newstr
,这意味着newstr[2]==somethingtrandom
请尝试以下方法
#include <stdio.h>
#include <stdlib.h>
char * itoa( int x )
{
const unsigned int BASE = 10;
unsigned int u = abs( x );
size_t n = 0;
unsigned int tmp = u;
char *s;
do { ++n; } while ( tmp /= BASE );
n += x < 0;
s = malloc( ( n + 1 ) * sizeof( char ) );
s[n] = '\0';
do
{
s[--n] = u % BASE + '0';
} while ( u /= BASE );
if ( x < 0 ) s[--n] = '-';
return s;
}
int main(void)
{
char *s = itoa( -1234567890 );
printf( "%s\n", s );
free( s );
return 0;
}
至于你的问题,你的代码出了什么问题,那就完全错了。:)
例如,该代码忽略等于零的数字。变量指针未初始化
int pointer, i = 0;
每个循环处理一些垃圾等等
如果您调查我的代码,将对您更有帮助。请尝试以下方法
#include <stdio.h>
#include <stdlib.h>
char * itoa( int x )
{
const unsigned int BASE = 10;
unsigned int u = abs( x );
size_t n = 0;
unsigned int tmp = u;
char *s;
do { ++n; } while ( tmp /= BASE );
n += x < 0;
s = malloc( ( n + 1 ) * sizeof( char ) );
s[n] = '\0';
do
{
s[--n] = u % BASE + '0';
} while ( u /= BASE );
if ( x < 0 ) s[--n] = '-';
return s;
}
int main(void)
{
char *s = itoa( -1234567890 );
printf( "%s\n", s );
free( s );
return 0;
}
至于你的问题,你的代码出了什么问题,那就完全错了。:)
例如,该代码忽略等于零的数字。变量指针未初始化
int pointer, i = 0;
每个循环处理一些垃圾等等
如果您研究我的代码,将对您更有帮助。通过这种简单的方法,您可以做到这一点
int main()
{
char str [6] = "324";
printf ("%c",str[1]);
return 0;
}
用这个简单的方法你可以做到
int main()
{
char str [6] = "324";
printf ("%c",str[1]);
return 0;
}
OP的代码看起来非常接近于有限使用的正确性 不要以
value>0
测试开始,否则当value==0
时,结果将是“”
。请确保将迭代次数限制为不超过数组大小
do {
str[i] = value%10 + '0';
value /=10;
i++;
} while (value>0 && i < (6-1));
OP仍有2个剩余问题,OP尚未指定要执行的操作:
大于99999的值值<0 顺便说一句:“我认为“\0”是空的,对吗?”
“\0”
是“空字符”。这与“空指针”不同
“如何将其更改为获取整数并返回表示整数的字符数组的函数?” OP的代码有很多问题。建议一种新方法。
执行到本地数组的转换,然后返回其副本。
调用代码最终应该
free()
it
#include <limits.h>
// max size needed for int. Note 10/33 just greater than log(2)
#define INTSIZE_MAX (sizeof int * CHAR_BIT * 10 / 33 + 3)
char *intoa_alloc(int x) {
char buf[INTSIZE_MAX];
char *p = &buf[sizeof buf - 1];
// Work with negative absolute value to cope with INT_MIN
// This avoids portability problems with abs(x) approach.
int x_negative = x < 0 ? x : -x;
// Form string
*p = '\0';
do {
*--p = '0' - x_negative % 10;
x_negative /= 10;
} while (x_negative);
if (x < 0) {
*--p = '-';
}
char *dest = malloc(strlen(p) + 1);
if (dest) {
strcpy(dest, p);
}
return dest;
}
#包括
//整数所需的最大大小。注释10/33刚好大于对数(2)
#定义INTSIZE_MAX(int的大小*字符位*10/33+3)
字符*intoa_alloc(int x){
字符buf[INTSIZE_MAX];
char*p=&buf[sizeof buf-1];
//使用负绝对值处理INT_MIN
//这避免了abs(x)方法的可移植性问题。
int x_负=x<0?x:-x;
//表单字符串
*p='\0';
做{
*--p='0'-x_负%10;
x_负/=10;
}while(x_阴性);
if(x<0){
*--p='-';
}
char*dest=malloc(strlen(p)+1);
如果(目的地){
strcpy(dest,p);
}
返回目的地;
}
OP的代码看起来非常接近于有限使用的正确性
不要以value>0
测试开始,否则当value==0
时,结果将是“”
。请确保将迭代次数限制为不超过数组大小
do {
str[i] = value%10 + '0';
value /=10;
i++;
} while (value>0 && i < (6-1));
OP仍有2个剩余问题,OP尚未指定要执行的操作:
大于99999的值
值<0
顺便说一句:“我认为“\0”是空的,对吗?”“\0”
是“空字符”。这与“空指针”不同
“如何将其更改为获取整数并返回表示整数的字符数组的函数?”
OP的代码有很多问题。建议一种新方法。
执行到本地数组的转换,然后返回其副本。
调用代码最终应该free()
it
#include <limits.h>
// max size needed for int. Note 10/33 just greater than log(2)
#define INTSIZE_MAX (sizeof int * CHAR_BIT * 10 / 33 + 3)
char *intoa_alloc(int x) {
char buf[INTSIZE_MAX];
char *p = &buf[sizeof buf - 1];
// Work with negative absolute value to cope with INT_MIN
// This avoids portability problems with abs(x) approach.
int x_negative = x < 0 ? x : -x;
// Form string
*p = '\0';
do {
*--p = '0' - x_negative % 10;
x_negative /= 10;
} while (x_negative);
if (x < 0) {
*--p = '-';
}
char *dest = malloc(strlen(p) + 1);
if (dest) {
strcpy(dest, p);
}
return dest;
}
#包括
//整数所需的最大大小。注释10/33刚好大于对数(2)
#定义INTSIZE_MAX(int的大小*字符位*10/33+3)
字符*intoa_alloc(int x){
字符buf[INTSIZE_MAX];
char*p=&buf[sizeof buf-1];
//使用负绝对值处理INT_MIN
//这避免了abs(x)方法的可移植性问题。
int x_负=x<0?x:-x;
//表单字符串
*p='\0';
做{
*--p='0'-x_负%10;
x_负/=10;
}while(x_阴性);
if(x<0){
*--p='-';
}
char*dest=malloc(strlen(p)+1);
如果(目的地){
strcpy(dest,p);
}
返回目的地;
}
我需要将其转换为字符的ary。所以sprintf()帮不了我。我可以使用一个for(),它将用“null”来完成我的数组。有什么问题:if(str[i]!='\0')?@ShayShterncharnewstr[6]={0}
使用'\0'
初始化整个newstr
,而sprintf()
填充非'\0'<