C Windows編程中常用數據類型轉換

    C Windows編程中常用數據類型轉換

一.其它數據類型轉換成字符串型
1.       短整型(int)
char temp[256];
int i;
itoa(i,temp,10);//i轉換爲字符串放入temp中,最後一個數字表示十進制
itoa(i,temp,2);//按二進制方式轉換
itoa(i,temp,16);//按十六進制方式轉換
表頭文件  #include<stdlib.h>
定義函數  char* itoa(int nValue, char *sz, int nRadix);
函數說明  將數字i轉換爲字符串放入temp中,最後一個參數表示進制。
範例:
#include<stdio.h>
#include<stdlib.h>
void main()
{
       int i=12;
       char temp[256];
       itoa(i,temp,10);
       printf("temp=%s\n",temp);
       itoa(i,temp,2);
       printf("temp=%s\n",temp);
       itoa(i,temp,16);
       printf("temp=%s\n",temp);
}
運行結果:
temp=12
temp=1100
temp=c

 

2.       長整性(long)
char temp[256];
long l;
ltoa(l,temp,10);//l轉換爲字符串放入temp中,最後一個數字表示十進制
表頭文件  #include<stdlib.h>
定義函數  char* ltoa(long lnValue, char *sz, int nRadix);
函數說明  將數字l轉換爲字符串放入temp中,最後一個參數表示進制。
範例:
#include<stdio.h>
#include<stdlib.h>
void main()
{
       long i=600000;
       char temp[256];
       ltoa(i,temp,10);
       printf("temp=%s\n",temp);
}
運行結果:
temp=600000

 

3.       浮點數(float,double
表頭文件  #include<stdlib.h>
定義函數  char* fcvt(double dValue, int nDig, int *pnDec, int *pnSign);
函數說明  第一個參數是浮點數,第二個參數是轉換後保留的小數位數,第三個參數表示小數點的位置,第四個參數表示符號
範例://將浮點數轉換爲字符串
#include<stdio.h>
#include<stdlib.h>
void main()
{
       int decimal,sign;
       char *buffer;
       double source=3.1415926535;
       buffer=fcvt(source,7,&decimal,&sign);
       printf("source=%.10f\nbuffer=%s\ndecimal=%d\nsign=%d\n",source,buffer,decimal,sign);
}
運行結果:
source=3.1415926535
buffer=31415927
decimal=1
sign=0
說明:decimal表示小數點的位置,sign表示符號:0爲正數,1爲負數,

 

 

通用方法:用sprintf完成轉換:
char buffer[200];  
char c=’a’;                 
int i=35;                   
long l=1000000;               
double f=1.7320534f;         

 

sprintf(buffer,”%c”,c);
sprintf(buffer,”%d”,i);
sprintf(buffer,”%d”,l);
sprintf(buffer,”%f”,f);
範例:
#include<stdio.h>
void main()
{
       char buffer[200];
       char c='a';
       int t=123;
       long l=666666;
       double f=456e-2;
       sprintf(buffer,"%c",c);
       printf("buffer=%s\n",buffer);
       sprintf(buffer,"%d",t);
       printf("buffer=%s\n",buffer);
       sprintf(buffer,"%d",l);
       printf("buffer=%s\n",buffer);
       sprintf(buffer,"%f",f);
       printf("buffer=%s\n",buffer);
}
運行結果:
buffer=a
buffer=123
buffer=666666
buffer=4.560000

 

二.字符串轉換爲其它數據類型
Strcpy(temp,”123”);
1.       短整型(int)
i=atoi(temp);
表頭文件  #include<stdlib.h>
定義函數  int atoi(const char *nptr);
函數說明  atoi()會掃描參數nptr字符串,跳過前面的空格字符,直到遇上數字或正負符號纔開始做轉換,而再遇到非數字或字符串結束(‘\0’)時才結束轉換,並將結果返回。
返回值  返回轉換後的整型數。
範例://將字符串str1與字符串str2轉換成數字後相加
#include<stdio.h>
#include<stdlib.h>
void main()
{
       char str1[]="-1234";
       char str2[]="5678";
       int t1=atoi(str1);
       int t2=atoi(str2);
       int t3=t1+t2;
       printf("t1=%d\n",t1);
       printf("t2=%d\n",t2);
       printf("t3=%d\n",t3);
}
運行結果:
t1=-1234
t2=5678
t3=4444

 

2.       長整形(long)
l=atol(temp);
表頭文件  #include<stdlib.h>
定義函數  long atol(const char *nptr);
函數說明  atol()會掃描參數nptr字符串,跳過前面的空格字符,直到遇上數字或正負符號纔開始做轉換,而再遇到非數字或字符串結束(‘\0’)時才結束轉換,並將結果返回。
返回值  返回轉換後的長整型數。
範例://將字符串str1與字符串str2轉換成數字後相加
#include<stdio.h>
#include<stdlib.h>
void main()
{
       char str1[]="-12345678";
       char str2[]="3456789";
       long t1=atol(str1);
       long t2=atol(str2);
       long t3=t1+t2;
       printf("t1=%d\n",t1);
       printf("t2=%d\n",t2);
       printf("t3=%d\n",t3);
}
運行結果:
t1=-12345678
t2=3456789
t3=-8888889

 

3.       浮點(double)
d=atof(temp);
表頭文件  #include<stdlib.h>
定義函數  double atof(const char *nptr);
函數說明  atof()會掃描參數nptr字符串,跳過前面的空格字符,直到遇上數字或正負符號纔開始做轉換,而再遇到非數字或字符串結束(‘\0’)時才結束轉換,並將結果返回。參數nptr字符串可包含正負號、小數點或E(e)來表示指數部分,如123.456123e-2
返回值  返回轉換後的浮點型數。
範例://將字符串str1與字符串str2轉換成數字後相加
#include<stdio.h>
#include<stdlib.h>
void main()
{
       char str1[]="-123.45";
       char str2[]="34e2";
       double t1=atof(str1);
       double t2=atof(str2);
       double t3=t1+t2;
       printf("t1=%.2f\n",t1);
       printf("t2=%.2f\n",t2);
       printf("t3=%.2f\n",t3);
}
運行結果:
t1=-123.45
t2=3400.00
t3=3276.55
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章