C 資料型別轉換

2021-08-15 22:50:41 字數 2484 閱讀 4311

轉cstring:   cstring.format(_t("%d"), int);

轉char*:       1._itoa(int,char*,10)//10為十進位制  沒有越界檢查

2.memset(szbuf, 0, sizeof(szbuf));

_snprintf(szbuf,sizeof(szbuf), "%d", int);

轉tchar*:    _itoa(int,tchar*,10)//10為十進位制

轉char*:       1._gcvt(double,一共幾個字元,char*);//sprintf(char*,"%.2lf", double);

2.memset(char*, 0, sizeof(char*));

_snprintf(char*, sizeof(char*), "%f", double);

3.位數保留:兩位小數:double dxxtbkd = (long long)(dtemp * 100 + 0.5) / 100.0; //如果是3位100就變成1000

精度保留:     float spd = 22.518744;

charbuf[10];

sprintf(buf,"%.2f", spd);//float轉char

sscanf(buf,"%f", &spd);//char轉float

轉double:     1.double=atof (char*);//有溢位風險

2.sscanf(char*,"%f", &double);

轉int:            1.int=atoi(char*);//sscanf(szbuf,"%d",&i);

2. sscanf(szbuf,"%d",&i);

轉cstring:   cstring= string.c_str();

轉 char*:      char*=string.c_str();

轉int/double:#include

std::stringstreamss(string);

int:int n=0;ss>>n;

double:double d=0.0;ss>>d;

轉cstring:   cstring.format(_t("%s"),tchar*);/cstring.format(tchar*);

轉int:           int=_tstoi(tchar*);

拼接:             cstring3.format(_t("%s%s"),cstring1,cstring2);

轉int:            int=_tstoi(cstring);

轉double:    double=_wtof (cstring);

轉char*:       cstring str;

dworddwnum = widechartomultibyte(cp_oemcp,null,str,-1,null,null,0,null);

char*c = new char[dwnum];

widechartomultibyte(cp_oemcp,null,str,-1,c,dwnum,0,null);

轉string:    string = ct2a(cstring.getbuffer());//cstring. releasebuffer();

格式化:         qstring strprinf1.sprintf("\n總長度為%f",val);

轉char*:    char* ch;qstring str;

qbytearrayba = str.tolocal8bit().constdata();

ch= ba.data();

c++資料型別轉換詳解:

c 資料型別轉換

隱式型別轉換 這些轉換是 c 預設的以安全方式進行的轉換,不會導致資料丟失。例如,從小的整數型別轉換為大的整數型別,從派生類轉換為基類。轉換規則從儲存範圍小的型別到儲存範圍大的型別。整數具體規則為 byte short char int long float double也就是說byte型別的變數可...

C 資料型別轉換

include include using namespace std 從型別t轉換為型別k template classt,class k k convert t tmp intmain home hejinyang clion2016.2 system cmake generated mypro...

C 資料型別轉換

一 隱式資料型別轉換 由編譯器自動去轉換型別,比如byte型別轉換為int型別 int i 12 byte j 125 i j console.writeline i 125 請按任意鍵繼續.隱式型別轉換的條件 1 資料型別是相容的 2 目標型別一定要大於原型別 二 強制資料型別轉換 有可能造成資料...