幾個字串轉數位函數

來源:互聯網
上載者:User

atol、atoi、strtol類似,最後的l代表長整型,i代表int

atol(將字串轉換成長整型數)
相關函數
    atof,atoi,strtod,strtol,strtoul
表標頭檔
    #include<stdlib.h>
定義函數
    long atol(const char *nptr);
函數說明
    atol()會掃描參數nptr字串,跳過前面的空白字元,直到遇上數字或正負符號才開始做轉換,而再遇到非數字或字串結束時('/0')才結束轉換,並將結果返回。
傳回值
    返迴轉換後的長整型數。
附加說明
    atol()與使用strtol(nptr,(char**)NULL,10);結果相同。
範例
    /*將字串a與字串b轉換成數字後相加*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=” 234567890”;
long c;
c=atol(a)+atol(b);
printf(“c=%d/n”,c);
}
運行結果:
    c=1234567890




strtod(將字串轉換成浮點數)
相關函數
    atoi,atol,strtod,strtol,strtoul
表標頭檔
    #include<stdlib.h>
定義函數
    double strtod(const char *nptr,char **endptr);
函數說明
    strtod()會掃描參數nptr字串,跳過前面的空白字元,直到遇上數字或正負符號才開始做轉換,到出現非數字或字串結束時('/0')才結束轉換,並將結果返回。若endptr不為NULL,則會將遇到不合條件而終止的nptr中的字元指標由endptr傳回。參數nptr字串可包含加號或減號、小數點或E(e)來表示指數部分。如123.456或123e -2。
傳回值
    返迴轉換後的浮點型數。
附加說明
    參考atof()。
範例
    /*將字串a,b,c 分別採用10,2,16 進位轉換成數字*/
#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));
}
運行結果:
a=1000000000
b=512
c=65535

 

 

參考:http://www.17xie.com/read-406473.html 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.