ObjextARX-VS2005-字串轉換

來源:互聯網
上載者:User

標籤:des   io   使用   ar   for   檔案   資料   sp   amp   

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型。

 

4.ACHAR類型(此類型是AUTODESK公司在adachar.h 標頭檔中定義的-
typedef wchar_t ACHAR;)

#include "adachar.h"

 

5.USES_CONVERSION在atlconv.h標頭檔中定義

#include "atlconv.h "

 

6.string->ACHAR*
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);

 

 

7.ACHAR*->string
ACHAR* ach;
USES_CONVERSION;

string temp=W2A(ach);

 

8.(wchar_t)ACHAR*->char*
char * ch;
ACHAR* ach;
USES_CONVERSION;

ch=T2A(ach);

 

9.char*->ACHAR*
ACHAR* ach1;
char * ch;
方法一:
USES_CONVERSION;
ach1=A2W(ch);
方法二:
size_t convertedChars=0;//記錄返回實際轉換字串的長度
mbstowcs_s(&convertedChars,ach1,10,ch,_TRUNCATE);//10為ch的最大長

度,隨著需要而改變

 

10.int->string
string str;
int nNumber=10001;
char cT[10];//把int轉化為string
_itoa_s(nNumber,cT,10);

str=cT;或string str(cT);

 

11.string->int

 

12.CString->char*
方法一:
char* ch;
CString temp;
ch=T2A(temp.GetBuffer(0));
方法二:
使用強制轉換
CString theString( "This is a test" );

LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

 

13.char*->CString
方法一:
可以直接賦值
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));

 

14.char*->int
char* ch;

int n=atoi(ch);

 

15.int->char*
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);

 

16.CString->string
string str;
CString temp;
USES_CONVERSION;

str=T2A(temp.GetBuffer(0));

 

17.string->CString
CString cstr;
string str;

cstr=str.c_str();

 

18.char*->string
直接構造法:
char cT1[20];

string ste(cT1);//重新構造一個字串

 

19.string->char*
string ste;
ch=(char*)ste.c_str();通過char*轉換去掉const屬性,注意ch只能為

char*,不能為char[]

 

20.float(double)->string
char cT1[20];
_gcvt_s(cT1,20,110.58485678,6);//6為精確度

string ste(cT1);

 

21.string->double
string num="15.12054";
double d=atof(num.c_str());

ObjextARX-VS2005-字串轉換

聯繫我們

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