一起來學(xué)習(xí)C語言的字符串轉(zhuǎn)換函數(shù)
字符串轉(zhuǎn)整數(shù)
字符串轉(zhuǎn)換為整數(shù)的函數(shù)有兩個(gè),他們的函數(shù)原型如下:
int __cdecl atoi(const char *_Str); long __cdecl atol(const char *_Str);
這兩個(gè)函數(shù)的用法都很簡單,atoi函數(shù)將字符串轉(zhuǎn)換為整數(shù)int
型,atol函數(shù)將字符串轉(zhuǎn)換為長整形long int
型。如果轉(zhuǎn)換無效,返回值都為0,下面通過一個(gè)簡單的例子看一下這兩個(gè)函數(shù)的用法。
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int val; long val_l; char str[20]; strcpy(str, "1234"); val = atoi(str); printf("字符串值 = %s, 整型值 = %d\n", str, val); strcpy(str, "abc"); val = atoi(str); printf("字符串值 = %s, 整型值 = %d\n", str, val); strcpy(str, "98993489"); val_l = atol(str); printf("\n字符串值 = %s, 長整型值 = %ld\n", str, val_l); strcpy(str, "abc123"); val_l = atol(str); printf("字符串值 = %s, 長整型值 = %ld\n", str, val_l); return 0; }
給字符串賦不同的值,然后使用,atoi函數(shù)和atol函數(shù)對字符串進(jìn)行轉(zhuǎn)換,輸出結(jié)果如下:
通過結(jié)果可以看出,只有當(dāng)字符串有效時(shí)才能正確的轉(zhuǎn)換成整數(shù),否則轉(zhuǎn)換的結(jié)果就為0。
將字符串轉(zhuǎn)換為整數(shù)的函數(shù)還有以下兩個(gè):
long __cdecl strtol(const char * __restrict__ _Str,char ** __restrict__ _EndPtr,int _Radix); unsigned long __cdecl strtoul(const char * __restrict__ _Str,char ** __restrict__ _EndPtr,int _Radix);
strtol() 函數(shù)用來將字符串轉(zhuǎn)換為長整型數(shù)(long
),它有三個(gè)參數(shù):
_Str
為要轉(zhuǎn)換的字符串,_EndPtr
為第一個(gè)不能轉(zhuǎn)換的字符的指針,_Radix
為字符串 str 所采用的進(jìn)制。
trtol() 函數(shù)會(huì)將參數(shù)str
字符串根據(jù)參數(shù)base
來轉(zhuǎn)換成長整型數(shù)(long
)。參數(shù)base
范圍從2
至36
,或0
。參數(shù)base
代表str
采用的進(jìn)制方式,如base
值為10
則采用10
進(jìn)制,若base
值為16
則采用16
進(jìn)制等。
下面通過一個(gè)簡單的例子演示一下strtol() 函數(shù)的用法。
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { char str[30] = "123 -456 abc"; char *pEnd; long ret1,ret2; ret1 = strtol(str, &pEnd, 10); ret2 = strtol(pEnd, &pEnd, 10); printf("數(shù)字1是: %ld\n", ret1); printf("數(shù)字2是: %ld\n", ret2); printf("字符串部分是: %s \n", pEnd); return 0; }
定義一個(gè)字符串,然后使用strtol() 函數(shù)轉(zhuǎn)換字符串中的數(shù)字,輸出結(jié)果如下:
通過打印的結(jié)果可以看出,strtol() 函數(shù)將字符串中的兩個(gè)數(shù)字都成功換成為整數(shù)了。
當(dāng)strtol() 函數(shù)讀取字符串時(shí),讀取到第一個(gè)空格是,由于空白字符不能轉(zhuǎn)換,所以函數(shù)返回,并將空白字符的存儲在 pEnd
中,接下來從空白位置繼續(xù)轉(zhuǎn)換,將字符串“-456”
轉(zhuǎn)換為數(shù)字之后,又遇到了空白字符,不能繼續(xù)轉(zhuǎn)換,函數(shù)退出,將第二個(gè)空白字符存儲在 pEnd
中,最后將剩余的字符串換打印出來。
通過strtol() 函數(shù)就可以智能的將字符串的數(shù)字提取出來。
strtoul 函數(shù)用來將字符串轉(zhuǎn)換成無符號長整型數(shù)(unsigned long
),它的用法和strtol() 函數(shù)基本一樣。
修改上面的代碼如下:
#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { char str[30] = "123 456 abc"; char *pEnd; long ret1,ret2; ret1 = strtoul(str, &pEnd, 10); ret2 = strtoul(pEnd, &pEnd, 10); printf("數(shù)字1是: %lu\n", ret1); printf("數(shù)字2是: %lu\n", ret2); printf("字符串部分是: %s \n", pEnd); return 0; }
輸出結(jié)果為:
如果在字符串的數(shù)字前面添加上負(fù)號,轉(zhuǎn)換的結(jié)果就會(huì)出錯(cuò)。
在使用strtol() 函數(shù)和strtoul 函數(shù)時(shí)要注意兩點(diǎn):
- 當(dāng)
base
的值為0
時(shí),默認(rèn)采用10
進(jìn)制轉(zhuǎn)換,但如果遇到'0x' / '0X'
前置字符則會(huì)使用16
進(jìn)制轉(zhuǎn)換,遇到'0'
前置字符則會(huì)使用8
進(jìn)制轉(zhuǎn)換。 - 若
endptr
不為NULL
,則會(huì)將遇到的不符合條件而終止的字符指針由endptr
傳回;若endptr
為NULL
,則表示該參數(shù)無效,或不使用該參數(shù)。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vscode+platformIO開發(fā)stm32f4的實(shí)現(xiàn)
這篇文章主要介紹了vscode+platformIO開發(fā)stm32f4的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05C語言數(shù)據(jù)結(jié)構(gòu)旋轉(zhuǎn)鏈表的實(shí)現(xiàn)
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)旋轉(zhuǎn)鏈表的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08C++ 關(guān)于MFC List Control 控件的總結(jié)
這篇文章主要介紹了C++ 關(guān)于MFC List Control 控件的總結(jié)的相關(guān)資料,十分的詳細(xì),有需要的朋友可以參考下2015-06-06