最近编程一直头痛字集中类型的转化,明知都可以转却总是记不住,不断的上网查来查去,在这里小结一下。以备以后方便使用,当然有些方法可能不是最新的,或者最简单的,但是对于自己已经了解的使用起来应该方便的多:
wstring s2ws(const string& s) { _bstr_t t = s.c_str(); wchar_t* pwchar = (wchar_t*)t; wstring result = pwchar; return result; }
string ws2s(const wstring& ws) { _bstr_t t = ws.c_str(); char* pchar = (char*)t; string result = pchar; return result; }
CString.format("%s", string.c_str()); //用c_str()确实比data()要好.
或:
CString StringToCString(string str) { CString result; for (int i=0;i<(int)str.length();i++) { result+=str[i]; } return result; }
void ConvertCString2string(CString& strSrc,std::string& strDes) { #ifndef UNICODE strDes = strSrc; #else USES_CONVERSION; strDes = W2A(strSrc.LockBuffer()); strSrc.UnlockBuffer(); #endif }
或:
string s(CString.GetBuffer()); ReleaseBuffer(); GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
或:
string CStringToString(CString cstr) { string result(cstr.GetLength(),'e'); for (int i=0;i<cstr.GetLength();i++) { result[i]=(char)cstr[i]; } return result; }
char *p = string.c_str(); //举例: string aa("aaa"); char *c=aa.c_str(); string mngName; char t[200]; memset(t,0,200); strcpy(t,mngName.c_str());
或:
char *p = new char[sring的长度+1]; p[string的长度]='/0'; //但是要注意最后赋值'/0'!!! 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'; }
string s(char *); //你的只能初始化,在不是初始化的地方最好还是用assign(); string CharToString(char*arr,int count) { string result(arr,4); return result; } //string是ansi编码字符char //TCHAR是unicode编码字符wchar_t
/* 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; }
char * wch2chr(LPCTSTR lpString) { // Calculate unicode string length. UINT len = wcslen(lpString)*2; char *buf = (char *)malloc(len); UINT i = wcstombs(buf,lpString,len); return buf; }
/* string 转 int .............................. char* 转 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);
在stdlib.h中有个函数itoa()
//itoa(i,num,10); //i 需要转换成字符的数字 //num 转换后保存字符的变量 //10 转换数字的基数(进制)10就是说按照10进制转换数字。还可以是2,8,16等等你喜欢的进制类型 //原形:char *itoa(int value, char* string, int radix); #include "stdlib.h" #include "stdio.h" main() { int i=1234; char s[5]; itoa(i,s,10); printf("%s",s); getchar(); }
//std::wstring转CString CString str( filename.c_str() );
//CString转std::wstring std::wstring str = filename.GetString();
CString cstr(asdd); const char* ch = (LPCTSTR)cstr; //举例: CString str= "i am good "; char* lp=str.GetBuffer(str.GetLength());
*********************************************************************** * 函数: THCAR2Char * 描述:将TCHAR* 转换为 char* *********************************************************************** char* CPublic::THCAR2char(TCHAR* tchStr) { int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度 char* chRtn = new char[iLen+1] wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值 return chRtn; }
定义了UNICODE宏之后,TCHAR就是宽字符wchar_t,否则TCHAR跟char是一样的^_
str.ReleaseBuffer();
int iint; CString csstr; csstr.Format("%d",iint);
CString.format("%s", char*); CString strtest; char * charpoint; charpoint="give string a value"; strtest=charpoint; //直接付值
CString ss="1212.12"; int temp=atoi(ss); //atoi _atoi64或atol //将字符转换为整数,可以使用atoi、_atoi64或atol。 int int_chage = atoi((lpcstr)ss) ; //或: CString str = "23"; UINT uint; sscanf(str, "%d", uint);
#include <iostream> #include <string> using namespace std; int main() { int n = 65535; char t[256]; string s; sprintf(t, "%d", n); s = t; cout << s << endl; return 0; }
或:
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int n = 65535; strstream ss; string s; ss << n; ss >> s; cout << s << endl; return 0; }
或:
//1.) String s = String.valueOf(i); //2.) String s = Integer.toString(i); //3.) String s = "" + i;
string sName = "string -> wchar_t"; wchar_t * wName = new wchar_t[sName.size()]; swprintf(wName, L"%S ", sName.c_str()); //注意大写S。。
wchar_t wchart[] = L"wchar_t -> string"; wstring ws = wchart; _bstr_t t = ws.c_str(); char* pchar = (char*)t; string result = pchar;
原文:http://www.tiham.com/2011/12/string-wstring-cstring-char-tchar-int-dword.html