c语言常用数据类型转换整理
你要發(fā)送原始數(shù)據(jù)流?還是?格式化輸出?
如果是格式化?按原子說的?,用sprintf?/?printf;
如果發(fā)送原始內(nèi)存數(shù)據(jù)流,?可按下面發(fā)送,
發(fā)送?
#define BYTE0(pointer) (*((char*)(&pointer)+0)); #define BYTE1(pointer) (*((char*)(&pointer)+1)); #define BYTE2(pointer) (*((char*)(&pointer)+2)); #define BYTE3(pointer) (*((char*)(&pointer)+3));
上位機(jī)接到的?十六進(jìn)制碼,再按同樣格式轉(zhuǎn)回來。
?如 分別接收到 ?s1[4] s2[8] 里面, 如果是float 就是4個字節(jié)
*(float*)&s1 就是 a
*(double*)&s2 就是b
?
atof(將字符串轉(zhuǎn)換成浮點(diǎn)型數(shù))
atoi(將字符串轉(zhuǎn)換成整型數(shù))
atol(將字符串轉(zhuǎn)換成長整型數(shù))
strtod(將字符串轉(zhuǎn)換成浮點(diǎn)數(shù))
strtol(將字符串轉(zhuǎn)換成長整型數(shù))
strtoul(將字符串轉(zhuǎn)換成無符號長整型數(shù))
toascii(將整型數(shù)轉(zhuǎn)換成合法的ASCII 碼字符)
toupper(將小寫字母轉(zhuǎn)換成大寫字母)
tolower(將大寫字母轉(zhuǎn)換成小寫字母)
???? atof(將字符串轉(zhuǎn)換成浮點(diǎn)型數(shù))
相關(guān)函數(shù) atoi,atol,strtod,strtol,strtoul
表頭文件 #include <stdlib.h>
定義函數(shù) double atof(const char *nptr);
函數(shù)說明 atof()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)
???? 字或正負(fù)符號才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時
???? ('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。參數(shù)nptr字符串可包含正負(fù)
???? 號、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分,如123.456或123e-2。
?返回值 返回轉(zhuǎn)換后的浮點(diǎn)型數(shù)。
附加說明 atof()與使用strtod(nptr,(char**)NULL)結(jié)果相同。
? 范例 /* 將字符串a(chǎn) 與字符串b轉(zhuǎn)換成數(shù)字后相加*/
#include<stdlib.h>main(){char *a=”-100.23”;char *b=”200e-2”;float c;c=atof(a)+atof(b);printf(“c=%.2f\n”,c);}執(zhí)行 c=-98.23
?
???? atoi(將字符串轉(zhuǎn)換成整型數(shù))
相關(guān)函數(shù) atof,atol,atrtod,strtol,strtoul
表頭文件 #include<stdlib.h>
定義函數(shù) int atoi(const char *nptr);
函數(shù)說明 atoi()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)
???? 字或正負(fù)符號才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時
???? ('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。
?返回值 返回轉(zhuǎn)換后的整型數(shù)。
附加說明 atoi()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
? 范例 /* 將字符串a(chǎn) 與字符串b轉(zhuǎn)換成數(shù)字后相加*/
#include<stdlib.h>mian(){char a[]=”-100”;char b[]=”456”;int c;c=atoi(a)+atoi(b);printf(c=%d\n”,c);}執(zhí)行 c=356
?
???? atol(將字符串轉(zhuǎn)換成長整型數(shù))
相關(guān)函數(shù) atof,atoi,strtod,strtol,strtoul
表頭文件 #include<stdlib.h>
定義函數(shù) long atol(const char *nptr);
函數(shù)說明 atol()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上數(shù)
???? 字或正負(fù)符號才開始做轉(zhuǎn)換,而再遇到非數(shù)字或字符串結(jié)束時
???? ('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。
?返回值 返回轉(zhuǎn)換后的長整型數(shù)。
附加說明 atol()與使用strtol(nptr,(char**)NULL,10);結(jié)果相同。
? 范例 /*將字符串a(chǎn)與字符串b轉(zhuǎn)換成數(shù)字后相加*/
#include<stdlib.h>main(){char a[]=”1000000000”;char b[]=” 234567890”;long c;c=atol(a)+atol(b);printf(“c=%d\n”,c);}執(zhí)行 c=1234567890
?
???? gcvt(將浮點(diǎn)型數(shù)轉(zhuǎn)換為字符串,取四舍五入)
相關(guān)函數(shù) ecvt,fcvt,sprintf
表頭文件 #include<stdlib.h>
定義函數(shù) char *gcvt(double number,size_t ndigits,char *buf);
函數(shù)說明 gcvt()用來將參數(shù)number轉(zhuǎn)換成ASCII碼字符串,參數(shù)ndigits表示
???? 顯示的位數(shù)。gcvt()與ecvt()和fcvt()不同的地方在于,gcvt()所
???? 轉(zhuǎn)換后的字符串包含小數(shù)點(diǎn)或正負(fù)符號。若轉(zhuǎn)換成功,轉(zhuǎn)換后的字
???? 符串會放在參數(shù)buf指針?biāo)傅目臻g。
?返回值 返回一字符串指針,此地址即為buf指針。
附加說明
范例 #include<stdlib.h>main(){double a=123.45;double b=-1234.56;char *ptr;int decpt,sign;gcvt(a,5,ptr);printf(“a value=%s\n”,ptr);ptr=gcvt(b,6,ptr);printf(“b value=%s\n”,ptr);}執(zhí)行 a value=123.45b value=-1234.56
?
???? strtod(將字符串轉(zhuǎn)換成浮點(diǎn)數(shù))
相關(guān)函數(shù) atoi,atol,strtod,strtol,strtoul
表頭文件 #include<stdlib.h>
定義函數(shù) double strtod(const char *nptr,char **endptr);
函數(shù)說明 strtod()會掃描參數(shù)nptr字符串,跳過前面的空格字符,直到遇上
???? 數(shù)字或正負(fù)符號才開始做轉(zhuǎn)換,到出現(xiàn)非數(shù)字或字符串結(jié)束時
???? ('\0')才結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若endptr不為NULL,則會將遇
???? 到不合條件而終止的nptr中的字符指針由endptr傳回。參數(shù)nptr字
???? 符串可包含正負(fù)號、小數(shù)點(diǎn)或E(e)來表示指數(shù)部分。如123.456或
???? 123e-2。
?返回值 返回轉(zhuǎn)換后的浮點(diǎn)型數(shù)。
附加說明 參考atof()。
? 范例 /*將字符串a(chǎn),b,c 分別采用10,2,16 進(jìn)制轉(zhuǎn)換成數(shù)字*/
#include<stdlib.h>mian(){char a[]=”1000000000”;char b[]=”1000000000”;char c[]=”ffff”;printf(“a=%d\n”,strtod(a,NULL,10));printf(“b=%d\n”,strtod(b,NULL,2));printf(“c=%d\n”,strtod(c,NULL,16));}執(zhí)行 a=1000000000b=512c=65535
?
???? strtol(將字符串轉(zhuǎn)換成長整型數(shù))
相關(guān)函數(shù) atof,atoi,atol,strtod,strtoul
表頭文件 #include<stdlib.h>
定義函數(shù) long int strtol(const char *nptr,char **endptr,int base);
函數(shù)說明 strtol()會將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成長整型數(shù)。參
???? 數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方式,如base
???? 值為10則采用10進(jìn)制,若base值為16則采用16進(jìn)制等。當(dāng)base值為0
???? 時則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符則會使用16進(jìn)制
???? 做轉(zhuǎn)換。一開始strtol()會掃描參數(shù)nptr字符串,跳過前面的空格
???? 字符,直到遇上數(shù)字或正負(fù)符號才開始做轉(zhuǎn)換,再遇到非數(shù)字或字
???? 符串結(jié)束時('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若參數(shù)endptr不為
???? NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返
???? 回。
?返回值 返回轉(zhuǎn)換后的長整型數(shù),否則返回ERANGE并將錯誤代碼存入errno
???? 中。
附加說明 ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
? 范例 /* 將字符串a(chǎn),b,c 分別采用10,2,16進(jìn)制轉(zhuǎn)換成數(shù)字*/
#include<stdlib.h>main(){char a[]=”1000000000”;char b[]=”1000000000”;char c[]=”ffff”;printf(“a=%d\n”,strtol(a,NULL,10));printf(“b=%d\n”,strtol(b,NULL,2));printf(“c=%d\n”,strtol(c,NULL,16));}執(zhí)行 a=1000000000b=512c=65535
?
???? strtoul(將字符串轉(zhuǎn)換成無符號長整型數(shù))
相關(guān)函數(shù) atof,atoi,atol,strtod,strtol
表頭文件 #include<stdlib.h>
定義函數(shù) unsigned long int strtoul(const char *nptr,char
???? **endptr,int base);
函數(shù)說明 strtoul()會將參數(shù)nptr字符串根據(jù)參數(shù)base來轉(zhuǎn)換成無符號的長整
???? 型數(shù)。參數(shù)base范圍從2至36,或0。參數(shù)base代表采用的進(jìn)制方
???? 式,如base值為10則采用10進(jìn)制,若base值為16則采用16進(jìn)制數(shù)
???? 等。當(dāng)base值為0時則是采用10進(jìn)制做轉(zhuǎn)換,但遇到如'0x'前置字符
???? 則會使用16進(jìn)制做轉(zhuǎn)換。一開始strtoul()會掃描參數(shù)nptr字符串,
???? 跳過前面的空格字符串,直到遇上數(shù)字或正負(fù)符號才開始做轉(zhuǎn)換,
???? 再遇到非數(shù)字或字符串結(jié)束時('\0')結(jié)束轉(zhuǎn)換,并將結(jié)果返回。若
???? 參數(shù)endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符
???? 指針由endptr返回。
?返回值 返回轉(zhuǎn)換后的長整型數(shù),否則返回ERANGE并將錯誤代碼存入errno
???? 中。
附加說明 ERANGE指定的轉(zhuǎn)換字符串超出合法范圍。
? 范例 參考strtol()
???? toascii(將整型數(shù)轉(zhuǎn)換成合法的ASCII 碼字符)
相關(guān)函數(shù) isascii,toupper,tolower
表頭文件 #include<ctype.h>
定義函數(shù) int toascii(int c)
函數(shù)說明 toascii()會將參數(shù)c轉(zhuǎn)換成7位的unsigned char值,第八位則會被
???? 清除,此字符即會被轉(zhuǎn)成ASCII碼字符。
?返回值 將轉(zhuǎn)換成功的ASCII碼字符值返回。
范例 #include<stdlib.h>main(){int a=217;char b;printf(“before toascii () : a value =%d(%c)\n”,a,a);b=toascii(a);printf(“after toascii() : a value =%d(%c)\n”,b,b);}執(zhí)行 before toascii() : a value =217()
?
???? after toascii() : a value =89(Y)
???? tolower(將大寫字母轉(zhuǎn)換成小寫字母)
相關(guān)函數(shù) isalpha,toupper
表頭文件 #include<stdlib.h>
定義函數(shù) int tolower(int c);
函數(shù)說明 若參數(shù)c為大寫字母則將該對應(yīng)的小寫字母返回。
?返回值 返回轉(zhuǎn)換后的小寫字母,若不須轉(zhuǎn)換則將參數(shù)c值返回。
附加說明
? 范例 /* 將s字符串內(nèi)的大寫字母轉(zhuǎn)換成小寫字母*/
#include<ctype.h>main(){char s[]=”aBcDeFgH12345;!#$”;int i;printf(“before tolower() : %s\n”,s);for(i=0;I<sizeof(s);i++)s[i]=tolower(s[i]);printf(“after tolower() : %s\n”,s);}執(zhí)行 before tolower() : aBcDeFgH12345;!#$after tolower() : abcdefgh12345;!#$
?
toupper(將小寫字母轉(zhuǎn)換成大寫字母)
相關(guān)函數(shù) isalpha,tolower
表頭文件 #include<ctype.h>
定義函數(shù) int toupper(int c);
函數(shù)說明 若參數(shù)c為小寫字母則將該對映的大寫字母返回。
?返回值 返回轉(zhuǎn)換后的大寫字母,若不須轉(zhuǎn)換則將參數(shù)c值返回。
附加說明
? 范例 /* 將s字符串內(nèi)的小寫字母轉(zhuǎn)換成大寫字母*/
#include<ctype.h>main(){char s[]=”aBcDeFgH12345;!#$”;int i;printf(“before toupper() : %s\n”,s);for(i=0;I<sizeof(s);i++)s[i]=toupper(s[i]);printf(“after toupper() : %s\n”,s);}執(zhí)行 before toupper() : aBcDeFgH12345;!#$after toupper() : ABCDEFGH12345;!#$
?
轉(zhuǎn)載于:https://www.cnblogs.com/zxouxuewei/p/5022319.html
總結(jié)
以上是生活随笔為你收集整理的c语言常用数据类型转换整理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电影投资如果想在过年后潇洒一把来看看~
- 下一篇: 长开头的成语接龙大全