標籤:
原型:unsigned long int strtoul(const char *nptr,char **endptr,int base);相關函數 atof,atoi,atol,strtod,strtoul標頭檔:stdlib.h功能:將字串轉換成無符號長整型數說明:strtoul()會將參數nptr字串根據參數base來轉換成無符號的長整型數。參數base範圍從2至36,或0。參數base代表採用的進位方式,如base值為10則採用10進位,若base值為16則採用16進位數等。當base值為0時會根據情況選擇用哪種進位:如果第一個字元是‘0‘,就判斷第二字元如果是‘x’則用16進位,否則用8進位;第一個字元不是‘0’,則用10進位。一開始strtoul()會掃描參數nptr字串,跳過前面的空格字串,直到遇上數字或正負符號才開始做轉換,再遇到非數字或字串結束時(‘‘)結束轉換,並將結果返回。若參數endptr不為NULL,則會將遇到不合條件而終止的nptr中的字元指標由endptr返回。
附加說明 ERANGE指定的轉換字串超出合法範圍。
返回值:返迴轉換後的長整型數,否則返回ERANGE並將錯誤碼存入errno中。
參數:nptr:被轉換的字串endptr:將遇到不合條件而終止的nptr中的字元指標返回base:進位
應用執行個體:
#include <stdlib.h>#include <stdio.h>int main(){ long c = 0; char *endptr; c = strtoul("200e-2",&endptr,10); printf("原始字串:200e-2\t轉換後:%d\t返回字串:%s\n",c,endptr); c = strtoul("-123%^456",&endptr,10); printf("原始字串:-123%^456\t轉換後:%d\t返回字串:%s\n",c,endptr); c = strtoul("#$%123%^098",&endptr,10); printf("原始字串:#$%123%^098\t轉換後:%d\t返回字串:%s\n",c,endptr); c = strtoul(" ",&endptr,2); printf("原始字串: \t轉換後:%d\t返回字串:%s\n",c,endptr); c = strtoul("0.55",&endptr,10); printf("原始字串:0.55\t轉換後:%d\t返回字串:%s\n",c,endptr); c = strtoul("-145789632",&endptr,10); printf("原始字串:-145789632\t轉換後:%d\t返回字串:%s\n",c,endptr); c = strtoul("$#@!",&endptr,10); printf("原始字串:$#@!\t轉換後:%d\t返回字串:%s\n",c,endptr); return 0;}運行結果:
strtoul字串轉換函式