软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 电脑教程 > 编程开发 > C/C++开发 > c/c++不同字符类型转换

c/c++不同字符类型转换

1.
1.1 char*2int
char *numstr = “1234″; int val = atoi(numstr);

1.2 char*2double
char *numstr = “12.34″; double val = atof(numstr);

2.CString2LPCTSTR
char *p=”fdl”;
CString strd(p);
LPCTSTR lps = (LPCTSTR)strd;

3. LPCTSTR2CString
LPCTSTR lpctStr;
CString strTMP=lpctStr;//不要用CStringA

4. CString2string
1)
CString strMfc=”test”;
std::string strStl;
strStl=strMfc.GetBuffer(0);
2)
//wchar_t2char定义见wchar_t2char中
CString tem_cs=_T(“fds”);
string * tem_s;
wchar_t * wchar_t_c;
char * char_c=new char(100);
wchar_t_c=tem_cs.GetBuffer(0);
wchar_t2char(wchar_t_c,char_c);
tem_s=new string(char_c);

5.string2CString
CString strMfc;
std::string strStl=”test”;
strMfc=strStl.c_str();

6.string2LPCTSTR
1)
string s(“sdaf”);
CString cs;
cs=s.c_str();
LPCTSTR lp=(LPCTSTR)cs;
2)
char* string2LPCTSTR::Convert(string sFrom)
{
_TCHAR* sBuff = new _TCHAR[sFrom.length() + 2];
int iLength = 0;
iLength = wsprintf (sBuff,sFrom.c_str ());
sBuff[iLength+1] = ‘\0′;
return sBuff;
}

7.int2LPCTSTR
int i=3;
CString display_id_str;
display_id_str.Format(_T(“%d”),i);
LPCTSTR display_id=display_id_str;

8.int2CString
int i=3;
CString display_id_str;
display_id_str.Format(_T(“%d”),i);

9CString2int
1)
CString ss=”1212″;
int temp=atoi(ss);
2)wchar_t
CString ss=”1212″;
int temp=_ttoi(ss);

11wchar_t2char
1)
wchar_t* wchar_t_c=_T(“dsdsds”);
char* char_c=new char(100);
public void wchar_t2char(wchar_t* wchar_t_c,char* char_c)
{
int nLen = WideCharToMultiByte( CP_ACP, 0, wchar_t_c, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
return ;
}
WideCharToMultiByte( CP_ACP, 0, wchar_t_c, -1, char_c, nLen, NULL, NULL );
}
2)
wchar_t wchar_t_c[1024]={“wchar_t_c”};
char char_c[2048]={“char_c”};
swprintf(wchar_t_c,L”%s”,char_c);
sprintf(char_c,”%s”,wchar_t_c);

12string2char*
1)

2)
string s=”ds”;
const char *p = s.c_str();

13.char*2string
char * t=”ses”;
string s(t);
14char2wchar_t
wchar_t* wchar_t_c=new wchar_t(100);
char* char_c=”dsdsds”;
int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, char_c, -1, NULL, 0 );
if (nLen == 0)
{
return ;
}
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, char_c, -1, wchar_t_c, nLen );

15CString2char*
CString cs=”ds”;
char * c=cs.GetBuffer(0);

16CString2wchar_t*
CString cs=_T(“ds”);
wchar_t * c=cs.GetBuffer(0);

17char*2CString
char * d=”dsf”;
CString s;
s.Format(“%s”,d);

18int2string
int cargo_id_i=3;
CString tem_cs;
tem_cs.Format(“%d”,cargo_id_i);
string cargo_id=tem_cs.GetBuffer(0);

来源:http://cffile.sinaapp.com/?p=75

    相关阅读
    栏目导航
    推荐软件