c语言整型转字符型
1. 如何用c语言把整形转换成字符型
使用itoa函数。
原型:extern char *itoa(int i);
参考代码:
#include<stdio.h>
#include<stdlib.h>
intmain()
{
inta=125;
charb[50];
printf("%s ",itoa(a,b,10));//把10进制的125转成字符并输出。
return0;
}
/*
(1)c语言整型转字符型扩展阅读:
注意事项
itoa() 函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数(进制)。在上例中,转换基数为10,就意味着以10为转换进制。10:十进制;2:二进制。
itoa 并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
标准库中有sprintf,功能比这个更强,用法跟printf类似:
2. 高手请进!如何把整形数据转换为字符串(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;
}
3. C语言如何将一个整型数值转化为字符
int iValue;//整型数
char sz[10];//字符串
sprintf(sz, "%d", iValue);//这句需要头文件#include <stdio.h>
/*或者*/
itoa(iValue, sz, 10); //这句需要头文件#include <ctype.h>
sprintf类似于printf,printf比sprintf少第一个参数,就是直接在输出界面输出相应的东西,而sprintf就是将你要输出的东西按相应格式存放到第一个参数的字符串中。
itoa是直接将整型数转化成字符串
4. C语言中怎么把Int型转成字符型
要先弄清楚到底是将整型转换成字符还是一个字符串,如果把整型变量转为以该变量作为ascii码的字符,可以直接付值,当然这个数值要小于256,如果是转换成一个字符串,那就如上说讲的用sprintf或者itoa函数来实现吧
5. 用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]='