1. 程式人生 > >把int轉換為char把int轉換為char

把int轉換為char把int轉換為char

char   *ultoa(unsigned   long   value,char   *string,int   radix)  
將無符號整型數value轉換成字串並返回該字串,radix為轉換時所用基數  
char   *ltoa(long   value,char   *string,int   radix)  
將長整型數value轉換成字串並返回該字串,radix為轉換時所用基數  
char   *itoa(int   value,char   *string,int   radix)
將整數value轉換成字串存入string,radix為轉換所用基數.  
double atof(char   *nptr)                  
將字串nptr轉換成雙精度數,並返回這個數,錯誤返回0  
int     atoi(char   *nptr)                              
將字串nptr轉換成整型數,     並返回這個數,錯誤返回0  
long     atol(char   *nptr)    
將字串nptr轉換成長整型數,並返回這個數,錯誤返回0  
double strtod(char   *str,char   **endptr)  
將字串str轉換成雙精度數,並返回這個數,  
long strtol(char   *str,char   **endptr,int   base)  
將字串str轉換成長整型數,   並返回這個數,

0~10間的數字,可以這樣

int   a   =   1;
char   b;
b   =   '0 '   +   a;

或者

int   a   =   22222222222;
char   str[4];
str[0]   =   (char)(a   &   0xff);
str[1]   =   (char)((a   > >   8)   &   0xff);
str[2]   =   (char)((a   > >   16)   &   0xff);
str[3]   =   (char)((a   > >   24)   &   0xff);

或者

int       a = 65; //65 = 0x00000041
char       b[4];  

//b[3]='/n'; 
sprintf(b, "%d ",a);

//memcpy(b,&a,4);

或者

unsigned   int   a   =   437567232;
unsigned   char   b[4];
for   (int   i   =   0;   i   <   4;   i++)
{
b[i]   =   *((char*)&a+i);
}