ObjextARX-VS2005-字符串轉換
1.使用string必須添加頭文件
#include"string"using namespace std;
2.使用CString必須添加頭文件(在非MFC工程中)
#include"afx.h"注意:當出現#error : Building MFC application with /MD[d] (CRT
dll version) requires MFC shared dll version. Please #define
_AFXDLL or do not use /MD[d]錯誤時,改工程設置:
Project|Properties|Configuration Properties|General|Use of MFC :
Use MFC in a Shared DLL(項目-〉屬性-〉use of MFC 改成use MFC in a
share dll)
3.wchar_t
wchar_t是C++的字符數據類型,char是8位字符類型,最多隻能包含256種字符,許多外文字符集所含的字符數目超過256個,字符型無法表示
wchar_t數據類型為16位,所能表示的字符數遠超char型。
typedef wchar_t ACHAR;)
#include "adachar.h"
#include "atlconv.h "
string str="string";
ACHAR* ach;
USES_CONVERSION;
ach=(ACHAR*)A2CT(str.c_str());
std::string str="something";
TCHAR *param=new TCHAR[str.size()+1];
param[str.size()]=0;
//As much as we'd love to, we can't use memcpy() because
//sizeof(TCHAR)==sizeof(char) may not be true:
std::copy(str.begin(),str.end(),param);
ACHAR* ach;
USES_CONVERSION;
string temp=W2A(ach);
char * ch;
ACHAR* ach;
USES_CONVERSION;
ch=T2A(ach);
ACHAR* ach1;
char * ch;
方法一:
USES_CONVERSION;
ach1=A2W(ch);
方法二:
size_t convertedChars=0;//記錄返回實際轉換字符串的長度
mbstowcs_s(&convertedChars,ach1,10,ch,_TRUNCATE);//10為ch的最大長
度,隨著需要而改變
string str;
int nNumber=10001;
char cT[10];//把int轉化為string
_itoa_s(nNumber,cT,10);
str=cT;或string str(cT);
11.string->int
方法一:
char* ch;
CString temp;
ch=T2A(temp.GetBuffer(0));
方法二:
使用強製轉換
CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;
方法一:
可以直接賦值
CString cstr;
char* ch;
cstr=ch;
方法二:
通過使用Format函數
char chArray[] = "This is a test";
CString cstr;
MBCS下(即沒定義UNICODE時):
cstr.Format(_T("%s"), chArray);
定義UNICODE時:
USES_CONVERSION;
cstr.Format(_T("%s"), A2W(chArray));
char* ch;
int n=atoi(ch);
int n=45;
char nCh[10];
char* ch;
itoa(n,nCh,16);//16為進製,可以取2,8,10,16等
也可以采用如下形式:ch=itoa(n,nCh,16);
VS2005環境用:_itoa_s(n,nCh,2);
string str;
CString temp;
USES_CONVERSION;
str=T2A(temp.GetBuffer(0));
CString cstr;
string str;
cstr=str.c_str();
直接構造法:
char cT1[20];
string ste(cT1);//重新構造一個字符串
string ste;
ch=(char*)ste.c_str();通過char*轉換去掉const屬性,注意ch隻能為
char*,不能為char[]
char cT1[20];
_gcvt_s(cT1,20,110.58485678,6);//6為精確度
string ste(cT1);
string num="15.12054";
double d=atof(num.c_str());
最後更新:2017-04-03 08:26:17