string、wstring、cstring、 char、 tchar、int转换方法
1、string轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)cstring
?| 1 | 方法一、 |
| 1 2 3 | CString str; string? str1; str.format("%s", str1.c_str()); |
| 1 | 方法二、 |
| 1 | CString str; |
| 1 | string? str1; |
| 1 | str = str1.c_str(); |
| 1 | 方法三、 |
| 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轉(zhuǎn)string
?| 1 | 方法一、 |
| 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 4 5 6 | 方法二、 ????CString str3; ????string s(str3.GetBuffer());? ????str3.ReleaseBuffer(); GetBuffer()后一定要ReleaseBuffer(),否則就沒有釋放緩沖區(qū)所占的空間. |
| 1 | ?? |
| 1 | 方法三、 |
| 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轉(zhuǎn)char *
?| 1 | 方法一、 |
| 1 2 3 4 | string str1="Hello"; ????????char *str2=const_cast<char*>(str1.c_str()); 方法二、 |
| 1 2 3 4 5 6 7 | string mngName; ?????char t[200]; ???memset(t,0,200); ??strcpy(t,mngName.c_str()); |
???????? 方法三、
??? 一個(gè)一個(gè)字符的賦值
???? char *p = new char[sring的長(zhǎng)度+1];
???? p[string的長(zhǎng)度]='/0';
??? 但是要注意最后賦值'/0'!!!
| 1 2 3 4 5 6 7 8 9 10 11 | 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* 轉(zhuǎn)string
???? 1、string s(char *); 你的只能初始化,在不是初始化的地方最好還是用assign();
???? 2、string CharToString(char*arr,int count)
????????? {
???????????? string result(arr,4);
???????????? return result;
????????? }
string是ansi編碼字符char
TCHAR是unicode編碼字符wchar_t
7、string轉(zhuǎn)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 *轉(zhuǎn)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* 轉(zhuǎn)int
?| 1 2 3 | string 轉(zhuǎn) int 方法一、 |
| 1 2 3 4 5 | string s = "0xffff"; ???int t =0; ???sscanf(s.c_str(),"%x",&t); 方法二、 |
| 1 2 3 4 5 6 7 8 9 10 11 12 | string str = "0xffff"; ????long t =0; ????t = strtol(str.c_str(), NULL, 16); char* 轉(zhuǎn) int ?#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轉(zhuǎn)char*和string
????? 在stdlib.h中有個(gè)函數(shù)itoa()
? itoa的用法:
? itoa(i,num,10);
i 需要轉(zhuǎn)換成字符的數(shù)字
num 轉(zhuǎn)換后保存字符的變量
11、wstring轉(zhuǎn)Csting
????? std::wstring轉(zhuǎn)CString
CString str( filename.c_str() );
12、Cstring轉(zhuǎn)wstring
?| 1 | 方法一、 |
| 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 | ?? |
| 1 | 方法二、 |
| 1 2 | CString str = _T("1234"); std::wstring wstr = (LPCWSTR)CStringW(str); |
13、Cstring轉(zhuǎn)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 *轉(zhuǎn)Cstring
?| 1 | 方法一、 |
| 1 2 3 | CString?? str; char?? pStr[100]; str.Format( "%s ",pStr); |
| 1 | 方法二、 |
| 1 2 3 | CString str; ????char *p = "hello"; ????str = p; |
15、TCHar轉(zhuǎn)char
***********************************************************************
* 函數(shù): THCAR2Char
* 描述:將TCHAR* 轉(zhuǎn)換為 char*
***********************************************************************
| 1 2 3 4 5 6 7 | char* THCAR2char(TCHAR* tchStr) { int iLen = 2*wcslen(tchStr);//CString,TCHAR漢字算一個(gè)字符,因此不用普通計(jì)算長(zhǎng)度 char* chRtn = new char[iLen+1] wcstombs(chRtn,tchStr,iLen+1);//轉(zhuǎn)換成功返回為非負(fù)值 return chRtn; } |
16、char轉(zhuǎn)tchar
定義了UNICODE宏之后,TCHAR就是寬字符wchar_t,否則TCHAR跟char是一樣的^_
?
具體問題具體分析,浮云啊,一切皆是浮云.....
以下摘錄自網(wǎng)絡(luò):
..............................................................
《C++標(biāo)準(zhǔn)函數(shù)庫(kù)》中說的?
有三個(gè)函數(shù)可以將字符串的內(nèi)容轉(zhuǎn)換為字符數(shù)組和C—string?
1、data(),返回沒有”\0“的字符串?dāng)?shù)組?
2、c_str(),返回有”\0“的字符串?dāng)?shù)組?
3、copy()
//將 單字節(jié)char* 轉(zhuǎn)換為 寬字節(jié) wchar*
| 1 2 3 4 5 6 7 8 9 10 11 | 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; } |
// 將 寬字節(jié)wchar_t* 轉(zhuǎn)換 單字節(jié)char*
| 1 2 3 4 5 6 7 8 9 10 11 | 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 轉(zhuǎn)換為寬字符 wstring
| 1 2 3 4 5 6 7 8 9 | 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; } |
總結(jié)
以上是生活随笔為你收集整理的string、wstring、cstring、 char、 tchar、int转换方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于NTDDI_VERSION,_WIN
- 下一篇: #define宏定义中的#,##,@#,