C/C++字串和數字互換方案

來源:互聯網
上載者:User

原文:http://www.cppblog.com/xiaozhuozhuo/archive/2007/07/23/28663.html

▲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類型Null 物件,
並將指定的內容插入該對象。此時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));
}

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

 

2007-07-24 09:40 by nickey

boost::lexical_cast

 

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

 

2008-09-06 03:25 by Robert

Could you write down:

char* itoa(int n, char* buf)

相關文章

聯繫我們

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