Ⅰ 在c语言中怎么把一个整数转化为字符串
你可以使用库函数 itoa()
# include
# include
void main (void)
{
int num = 100;
char str[25];
itoa(num, str, 10);//把int 类型的num 转换成 10 进制的字符串类型
printf("The number 'num' is %d and the string 'str' is %s. \n" ,
num, str);
}
Ⅱ C语言 将一个整数转换成一个字符串
atoi: 把字符串转换成整型数
itoa:把整数转换为字符串
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int number = 12345;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
哇塞要上面那么复杂吗
Ⅲ C语言 将输入整数转换成字符串输出
在这里先假设n的值为123。
*s = '0'+i%10; i%10相当于取i取的个位数,即数字3. 字符0 加上 整数3就是字符3.(这个要弄明白哦)。所以字符s的字为3.
itoa(i/10,s-1); i/10即 123/10 = 12. 迭代函数, 字符指针s后退一位,s-1 = 2;
同理推出 s-2 = 1.itoa(n,str+5); str的长度是7。
即:str[0],str[1],str[2],str[3],str[4],str[5],str[6].
str + 5 即把地址指向 str[5].
看函数itoa(),
n为 123 时
str[0] = '';
str[1] = '';
str[2] = '';
str[3] = '1';
str[4] = '2';
str[5] = '3';
str[6] = '';
Ⅳ 高手请进!如何把整形数据转换为字符串(C语言)
功 能:把一整数转换为字符串
用 法:char *itoa(int value, char *string, int radix);
详细解释:itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写.
参数:
value: 待转化的整数。
radix: 是基数的意思,即先将value转化为radix进制的数,范围介于2-36,比如10表示10进制,16表示16进制。
* string: 保存转换后得到的字符串。
返回值:
char * : 指向生成的字符串, 同*string。
备注:该函数的头文件是"stdlib.h"
程序例:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int number = 123456;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
注释:编译系统:VC++6.0,TC不支持。
我们可以这样构造itoa()
char* itoa(int i)
{
char *a=malloc(42); /* Enough for a 128 bit integer */
if (a) sprintf(a,"%d",i);
return a;
}
实现itoa函数的源代码
char *my_itoa(int num,char *str,int radix){
const char table[]="";
char *ptr = str;
bool negative = false;
if(num == 0){ //num=0
*ptr++='0';
*ptr='\0'; // don`t forget the end of the string is '\0'!!!!!!!!!
return str;
}
if(num<0){ //if num is negative ,the add '-'and change num to positive
*ptr++='-';
num*=-1;
negative = true;
}
while(num){
*ptr++ = table[num%radix];
num/=radix;
}
*ptr = '\0'; //if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char *start =(negative?str+1:str); //now start points the head of the string
ptr--; //now prt points the end of the string
while(start<ptr){
char temp = *start;
*start = *ptr;
*ptr = temp;
start++;
ptr--;
}
return str;
}
Ⅳ 用c语言怎么将整数转换成字符串
1.int/float to string/array:
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明。
● itoa():将整型值转换为字符串。
● ltoa():将长整型值转换为字符串。
● ultoa():将无符号长整型值转换为字符串。
● gcvt():将浮点型数转换为字符串,取四舍五入。
● ecvt():将双精度浮点型值转换为字符串,转换结果中不包含十进制小数点。
● fcvt():指定位数为转换精度,其余同ecvt()。
除此外,还可以使用sprintf系列函数把数字转换成字符串,其比itoa()系列函数运行速度慢
2. string/array to int/float
C/C++语言提供了几个标准库函数,可以将字符串转换为任意类型(整型、长整型、浮点型等)。
● atof():将字符串转换为双精度浮点型值。
● atoi():将字符串转换为整型值。
● atol():将字符串转换为长整型值。
● strtod():将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字。
● strtol():将字符串转换为长整值,并报告不能被转换的所有剩余数字。
● strtoul():将字符串转换为无符号长整型值,并报告不能被转换的所有剩余数字。
C语言实现:
#include
#include
void itoa (int n,char s[]);
//atoi 函数:将s转换为整形数
int main(void )
{
int n;
char s[100];
printf("Input n: ");
scanf("%d",&n);
printf("the string : ");
itoa (n,s);
return 0;
}
void itoa (int n,char s[])
{
int i,j,sign;
if((sign=n)<0)//记录符号
n=-n;//使n成为正数
i=0;
do{
s[i++]=n%10+'0';//取下一个数字
}
while ((n/=10)>0);//删除该数字
if(sign<0)
s[i++]='-';
s[i]='