strtoul字串轉換函式

來源:互聯網
上載者:User

標籤:

原型: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字串轉換函式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.