• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ▲1、C語言標準庫函數atoi()等。

            函數名: atoi
            功 能: 把字符串轉換成整型數
            用 法: int atoi(const char *nptr);
            程序例:
            #include <stdlib.h>
            int main(void)
            {
            int n;
            char *str = "435";
            n = atoi(str);
            printf("string = %s integer = %d\n", str, n);
            return 0;
            }

            其他相關函數——

            函數名: atof
            功 能: 把字符串轉換成浮點數
            用 法: double atof(const char *nptr);
            程序例:
            #include <stdlib.h>
            int main(void)
            {
            float f;
            char *str = "12345.67";
            f = atof(str);
            printf("string = %s float = %f\n", str, f);
            return 0;
            }

            函數名: atol
            功 能: 把字符串轉換成長整型數
            用 法: long atol(const char *nptr);
            程序例:
            #include <stdlib.h>
            int main(void)
            {
            long l;
            char *str = "98765432";
            l = atol(lstr);
            printf("string = %s integer = %ld\n", str, l);
            return(0);
            }

            ▲2、sprintf與Format構造字符串——

                sprintf和printf都是C的產物,用法幾乎一樣,只是前者打印到字符串,后者直接在命令行上輸出。
                int sprintf( char *buffer, const char *format [, argument] … );
                除了前兩個參數類型固定外,后面可以接任意多個參數。而它的精華,顯然就在第二個參數:格式化字符串(想想printf吧,一樣的)。例:
            #include <iostream>
            int main()
            {
                double a(3112);
                char s1[10],s2[10];
                sprintf(s1,"%d\n",(int)a);
                sprintf(s2,"$%.2lf",a);
                std::cout<<s1<<s2<<std::endl;
            }

                在C++里,也可用Format(CString) :
            /*VS2005中,項目/屬性/配置屬性里字符集設置為未配置*/
            #include <iostream>
            #define _AFXDLL
            #include <afx.h>
            int main()
            {
                double a(32);
                CString s;
                s.Format("$%.2lf",a);
                std::cout<<s<<std::endl;
            }

            ▲3、字符串流stringstream提供的轉換和/或格式化。

            #include <iostream>
            #include <vector>
            #include <string>
            #include <sstream>
            using namespace std;
            int main()
            {
                int num(435);
                string s;
                ostringstream mystream;
                mystream<<num<<"\n";
                /*創建一個名為mystream的ostringstream類型空對象,
            并將指定的內容插入該對象。此時mystream的內容是以下字符:
            435\n*/
                s=mystream.str();
                cout<<s;
            }

            #include <iostream>
            #include <vector>
            #include <string>
            #include <sstream>
            using namespace std;
            int main()
            {
                string s("435");
                int num;
                istringstream mystream(s);
                mystream>>num;/*num=435*/
                cout<<num<<endl;
            }

            ▲4、自己寫函數。

            /*串到數,實參如("435",&number)*/
            void getnumber_from_string(char nums[],int *p)
            {
                int i,k=strlen(nums);
                for(i=0,(*p)=0;i<k;++i)
                (*p)+=pow_10(k-i-1)*(*(nums+i)-48);
            }
            int pow_10(int k) /*10的k次方*/
            {
                return (k==0?1:10*pow_10(k-1));
            }

            ▲5、想沒到/~!親想到沒?望補充>>>>

            Posted on 2007-07-23 22:50 小王子 閱讀(4838) 評論(5)  編輯 收藏 引用 所屬分類: 分類1:C++文章

            Feedback

            # re: ★(原創)C/C++字符串和數字互換方案收集★  回復  更多評論   

            2007-07-24 00:34 by 隨風而過

            # re: ★(原創)C/C++字符串和數字互換方案收集★[未登錄]  回復  更多評論   

            2007-07-24 08:39 by 漂舟
            樓主收集的方法還真不少,
            偶補充一個,strtol, strtoul, strtod, 這一系列標準庫函數

            # re: ★(原創)C/C++字符串和數字互換方案收集★  回復  更多評論   

            2007-07-24 09:40 by nickey
            boost::lexical_cast

            # re: ★(原創)C/C++字符串和數字互換方案收集★  回復  更多評論   

            2007-07-24 09:54 by pass86
            強烈推薦boost::lexical_cast
            SO EASY.

            # re: ★(原創)C/C++字符串和數字互換方案收集★[未登錄]  回復  更多評論   

            2008-09-06 03:25 by Robert
            Could you write down:

            char* itoa(int n, char* buf)
            亚洲精品乱码久久久久久按摩| 欧美亚洲另类久久综合| 一极黄色视频久久网站| 国产精品免费看久久久| 一本久道久久综合狠狠躁AV| 三级韩国一区久久二区综合| 国产精品久久免费| 亚洲精品无码久久久久| 久久水蜜桃亚洲av无码精品麻豆| 久久亚洲精品国产精品婷婷| 大美女久久久久久j久久| 97精品久久天干天天天按摩| 久久久久人妻精品一区三寸蜜桃| 精品久久777| 日本福利片国产午夜久久| 免费一级做a爰片久久毛片潮| 久久福利青草精品资源站| 久久人人爽人人精品视频| 97久久天天综合色天天综合色hd| 亚洲精品无码久久久久AV麻豆| 久久天天躁狠狠躁夜夜avapp| 久久亚洲天堂| 国产精品青草久久久久福利99| 久久精品国产亚洲AV大全| 久久亚洲精品成人AV| 国产精品一区二区久久精品涩爱 | 91麻豆国产精品91久久久| 欧美伊香蕉久久综合类网站| 久久精品国产亚洲av影院| 久久国内免费视频| 久久影院久久香蕉国产线看观看| 久久国产精品-久久精品| 91久久精品91久久性色| 少妇久久久久久久久久| 久久人人爽人人人人爽AV| 国产亚洲美女精品久久久2020| 久久久久99精品成人片直播| 婷婷综合久久中文字幕蜜桃三电影| 热久久国产欧美一区二区精品| 亚洲国产成人久久精品99| 免费一级欧美大片久久网|