字符串之间的转换

1.               string转wstring

wstring s2ws(conststring& s)

{

 _bstr_t t = s.c_str();

 wchar_t* pwchar = (wchar_t*)t;

 wstring result = pwchar;

 return result;

}

2.               wstring转string

string ws2s(constwstring& ws)

{

 _bstr_t t = ws.c_str();

 char* pchar = (char*)t;

 string result = pchar;

 return result;

}

3.               string转cstring

a)CString.format("%s", string.c_str()); 

b)CString StringToCString(string str)

{

CString result;

for (inti=0;i<(int)str.length();i++)

{

 result+=str[i];

}

return result;

}

4.               cstring转string

a)void ConvertCString2string(CString&strSrc,std::string& strDes)

{

#ifndef UNICODE

 strDes = strSrc;

#elseUSES_CONVERSION;

 strDes = W2A(strSrc.LockBuffer());

 strSrc.UnlockBuffer();

#endif

}

b)

strings(CString.GetBuffer()); 

ReleaseBuffer();

GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

c)

stringCStringToString(CString cstr)

{

stringresult(cstr.GetLength(),'e');

for (inti=0;i<cstr.GetLength();i++)

{

 result[i]=(char)cstr[i];

}

return result;

}

5.               string转char *

a)char *p = string.c_str();

举例: stringaa("aaa"); char *c=aa.c_str(); string mngName; char t[200]; memset(t,0,200);strcpy(t,mngName.c_str());

b)一个一个字符的赋值

char *p = newchar[sring的长度+1];

p[string的长度]='/0';

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

char *StringToChar(string &str)

{

intlen=str.length();

char * p= newchar[len+1];

for (inti=0;i<len;i++)

{

p[i]=str[i];

}

p[len]='/0';

}

6.               char* 转string

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

stringCharToString(char*arr,int count)

{

stringresult(arr,4);

return result;

}

string是ansi编码字符char

TCHAR是unicode编码字符wchar_t

7.               string转TCHAR *

/*

  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, (constchar*)buffer, int(len), wBuf, int(wlen));

        return wBuf;

}

8.               TCHAR *转string

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;

}

9.               string 和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);

10.            int转char*和string

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

 itoa的用法:

 itoa(i,num,10);

 i 需要转换成字符的数字

 num 转换后保存字符的变量

11.            wstring转Csting

std::wstring转CString

CString str(filename.c_str() );

12.            Cstring转wstring

CString转std::wstring

std::wstring str =filename.GetString();

13.            Cstring转char *

CStringcstr(asdd);

const char* ch =(LPCTSTR)cstr;

举例:

CString   str= "i   am  good ";

char*   lp=str.GetBuffer(str.GetLength());

str.ReleaseBuffer();

14.            char *转Cstring

CString   str;

char   pStr[100];

str.Format("%s ",pStr);

15.            TCHar转char

char*CPublic::THCAR2char(TCHAR* tchStr)

{

int iLen =2*wcslen(tchStr);CString,TCHAR汉字算一个字符,因此不用普通计算长度

char* chRtn = newchar[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()

17.            int 转 CString:

CString.Format("%d",int);

18.            string 转 CString 

CString.format("%s",string.c_str()); 

用c_str()确实比data()要好.  

19.            char* 转 CString 

CString.format("%s",char*);

 CString strtest; 

 char * charpoint; 

 charpoint="give string avalue"; 

 strtest=charpoint; 直接付值

20.            CString 转 int

               CString  ss="1212.12"; 

               inttemp=atoi(ss); atoi _atoi64或atol

 将字符转换为整数,可以使用atoi、_atoi64或atol。 

int int_chage = atoi((lpcstr)ss) ;

或:

   CString str = "23";

               UINTuint;

                sscanf(str, "%d", uint);

21.            char* 转 int

 #include <stdlib.h>

   intatoi(const char *nptr);

 long atol(const char *nptr);

 long long atoll(const char *nptr);

 long long atoq(const char *nptr);

22.            CString 转 string

  string s(CString.GetBuffer()); 

  GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间. 

23.            char* 转 string 

 string s(char *); 

               你的只能初始化,在不是初始化的地方最好还是用assign().

24.            CString 转 char *

               CStringstrtest="wwwwttttttt";

 charpoint=strtest.GetBuffer(strtest.GetLength());

CString转换 char[100] 

 char a[100]; 

               CStringstr("aaaaaa"); 

 strncpy(a,(LPCTSTR)str,sizeof(a));

               CString  str="aaa";  

                char* ch;  

               ch=(char*)(LPCTSTR)str;

25.            int 转 char *

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

 itoa的用法:

 itoa(i,num,10);

 i 需要转换成字符的数字

 num 转换后保存字符的变量

               10转换数字的基数(进制)10就是说按照10进制转换数字。还可以是2,8,16等等你喜欢的进制类型

 原形:char *itoa(intvalue, 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();

}

26.            string 转 char * 

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());

标准C里没有string,char *==char []==string

可以用CString.Format("%s",char*)这个方法来将char *转成CString。要把CString转成char *,用操

作符(LPCSTR)CString就可以了。

cannot convertfrom 'const char *' to 'char *'

const char*c=aa.c_str(); 

string.c_str()只能转换成const char *

#include<string>

 使用CString必须使用MFC,并且不可包含<windows.h>

#define _AFXDLL

#include<afx.h>

using namespacestd;

27.            将 单字节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;

}

 

28.             将 宽字节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;

}

 

29.             将单字符 string 转换为宽字符 wstring

inline voidAscii2WideString( 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;

}

int _tmain(intargc, _TCHAR* argv[])

{

char*   pChar = “我喜欢char”;

wchar_t* pWideChar= L”我讨厌wchar_t”;

wchar_t   tagWideCharList[100] ;

char   ch = ‘A’;

char   tagChar[100] = {NULL};

CString   cStr;

std::string str;

 设置语言环境以便输出WideChar

setlocale(LC_ALL,”chs”);

30.              char* 转换 wchar_t*

               wchar_t未重载 << ,所以不可使用 cout << 输出

pWideChar = AnsiToUnicode( pChar );

 printf(”%ls”) 和 wprintf(L”%s”) 一致

printf( “%ls/n”, pWideChar );

31.             wchar_t* 转换 wchar_t[]

wcscpy (tagWideCharList, pWideChar );

wprintf( L”%s/n”,tagWideCharList );

32.             wchar_t[] 转换 wchar_t*

pWideChar =tagWideCharList;

wprintf( L”%s/n”,pWideChar );

33.             char 转换 string

str.insert( str.begin(), ch );

cout << str << endl;

34.             wchar_t* 转换 string

pWideChar = newwchar_t[str.length()];

swprintf(pWideChar, L”%s”, str.c_str());

wprintf( L”%s/n”,pWideChar );

35.             string 转换 char*

pChar =const_cast<char*>(str.c_str());

cout <<pChar << endl;

36.             char* 转换 string

str =std::string(pChar);

  cout 的 << 重载了string, 若printf 的话必须 printf(”%s”,str.c_str());

   而不可 print( “%s”, str); 因为 str 是个 string 类

cout << str<< endl;

37.             string 转换 char[]

str = “无聊啊无聊”;

strcpy( tagChar,str.c_str() );

printf( “%s/n”,tagChar );

38.             string 转换 CString;

cStr = str.c_str();

39.             CString 转换 string

str = string(cStr.GetBuffer(cStr.GetLength()));

40.             char* 转换 CString

cStr = pChar;

41.             CString 转换 char*

pChar = cStr.GetBuffer( cStr.GetLength() );

42.             CString 转换 char[]

strncpy( tagChar, (LPCTSTR)CString, sizeof(tagChar));

43.             CString 转换 wchar_t*

pWideChar =cStr.AllocSysString();

printf( “%ls/n”,pWideChar );

}

 


你可能感兴趣的:(c,String,null,buffer,mfc,语言)