0%

字符串转换

string、wstring、cstring、 char、 tchar、int转换方法

string、wstring、cstring、 char、 tchar、int转换方法

1、string转wstring

1
2
3
4
5
6
7
wstring s2ws(const string& s)
{
_bstr_t t = s.c_str();
wchar_t* pwchar = (wchar_t*)t;
wstring result = pwchar;
return result;
}

2、wstring转string

1
2
3
4
5
6
7
string ws2s(const wstring& ws)
{
_bstr_t t = ws.c_str();
char* pchar = (char*)t;
string result = pchar;
return result;
}

3、string转cstring

1
2
3
CString str;
string str1;
str.format("%s", str1.c_str());
1
2
3
CString str;
string str1;
str = str1.c_str();
1
2
3
4
5
6
7
8
9
CString StringToCString(string str)
{
CString result;
for (int i=0;i<(int)str.length();i++)
{
result+=str[i];
}
return result;
}

4、cstring转string

1
2
3
4
5
6
7
8
9
void ConvertCString2string(CString& strSrc,std::string& strDes)
{
#ifndef UNICODE
strDes = strSrc;
#else USES_CONVERSION;
strDes = W2A(strSrc.LockBuffer());
strSrc.UnlockBuffer();
#endif
}
1
2
3
CString str3;
string s(str3.GetBuffer());
str3.ReleaseBuffer();//GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
1
2
3
4
5
6
7
8
9
string CStringToString(CString cstr)
{
string result(cstr.GetLength(),'e');
for (int i=0;i<cstr.GetLength();i++)
{
result[i]=(char)cstr[i];
}
return result;
}

5、string转char *

1
2
string str1="Hello";
char *str2=const_cast<char*>(str1.c_str());
1
2
3
4
string mngName;
char t[200];
memset(t,0,200);
strcpy(t,mngName.c_str());

一个一个字符的赋值

1
2
char *p = new char[sring的长度+1];
p[string的长度]='/0';

但是要注意最后赋值’/0’!

1
2
3
4
5
6
7
8
9
10
char * StringToChar(string &str)
{
int len=str.length();
char * p= new char[len+1];
for (int i=0;i<len;i++)
{
p[i]=str[i];
}
p[len]='/0';
}

6、char* 转string

1、

1
string s(char *);//你的只能初始化,在不是初始化的地方最好还是用assign();

2、

1
2
3
4
5
string CharToString(char*arr,int count)
{
string result(arr,4);
return result;
}

string是ansi编码字符char
TCHAR是unicode编码字符wchar_t

7、string转TCHAR *

1
2
3
4
5
6
7
8
9
/* wBuf 申明为指针即可。*/
wchar_t *chr2wch(const char *buffer)
{
size_t len = strlen(buffer);
size_t wlen = MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), NULL, 0);
wchar_t *wBuf = new wchar_t[wlen + 1];
MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), wBuf, int(wlen));
return wBuf;
}

8、TCHAR *转string

1
2
3
4
5
6
7
char * wch2chr(LPCTSTR lpString)
{
UINT len = wcslen(lpString)*2;
char *buf = (char *)malloc(len);
UINT i = wcstombs(buf,lpString,len);
return buf;
}

9、string 和 char* 转int

string 转 int

1
2
3
string s = "0xffff";
int t =0;
sscanf(s.c_str(),"%x",&t);
1
2
3
string str = "0xffff";
long t =0;
t = strtol(str.c_str(), NULL, 16);

char* 转 int

1
2
3
4
5
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);

10、int转char*和string

在stdlib.h中有个函数itoa()

itoa的用法:

1
itoa(i,num,10);

i 需要转换成字符的数字
num 转换后保存字符的变量

11、wstring转Csting

std::wstring转CString

1
CString str( filename.c_str() );

12、Cstring转wstring

1
2
3
4
5
6
CString orig("Hello, World!");
size_t origsize = strlen(orig) + 1;
size_t convertedChars = 0;
wchar_t wcstring[MAX_PATH];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
1
2
CString str = _T("1234");
std::wstring wstr = (LPCWSTR)CStringW(str);

13、Cstring转char *

1
2
CString str("aaaaa");
char *p = (LPSTR)(LPCTSTR)str;
1
2
3
CString   str= "i   am   good ";
char* lp=str.GetBuffer(str.GetLength());
str.ReleaseBuffer();

14、char *转Cstring

1
2
3
CString   str;
char pStr[100];
str.Format( "%s ",pStr);
1
2
3
CString str;
char *p = "hello";
str = p;

15、TCHar转char

函数: THCAR2Char
描述:将TCHAR* 转换为 char*

1
2
3
4
5
6
7
char* THCAR2char(TCHAR* tchStr)
{
int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
char* chRtn = new char[iLen+1]
wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
return chRtn;
}

16、char转tchar
定义了UNICODE宏之后,TCHAR就是宽字符wchar_t,否则TCHAR跟char是一样的^_^
具体问题具体分析,浮云啊,一切皆是浮云…..
以下摘录自网络:
《C++标准函数库》中说的
有三个函数可以将字符串的内容转换为字符数组和C—string
1、data(),返回没有”\0“的字符串数组
2、c_str(),返回有”\0“的字符串数组
3、copy()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//将 单字节char* 转换为 宽字节 wchar*

inline wchar_t* AnsiToUnicode( const char* szStr )
{
int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
if (nLen == 0)
{
return NULL;
}
wchar_t* pResult = new wchar_t[nLen];
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
return pResult;
}


// 将 宽字节wchar_t* 转换 单字节char*

inline char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
return pResult;
}

// 将单字符 string 转换为宽字符 wstring

inline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr )
{
int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );
wszStr.resize(nLength);
LPWSTR lpwszStr = new wchar_t[nLength];
MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength );
wszStr = lpwszStr;
delete [] lpwszStr;
}