wchar_t,char,std::string,std::wstring,cstring Conversion

Source: Internet
Author: User

wchar_t*,wchar_t,wchat_t array, Char,char*,char array, std::string,std::wstring,cstring ....

Some conversion functions, mainly for wide characters. A string is a fundamental thing to master.


#include <string>
Use CString must use MFC and cannot contain <windows.h>
#define _afxdll
#include <afx.h>
using namespace Std;
———————————————————————————-
Converts a single-byte char* to a wide byte 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;
}
———————————————————————————-
Converts a wide-byte wchar_t* to a single byte 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;
}
———————————————————————————-
Converts a single character string to wide characters 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;
}
———————————————————————————-
int _tmain (int argc, _tchar* argv[])
{
char* Pchar = "I like char";
wchar_t* Pwidechar = l "I hate wchar_t";
wchar_t tagwidecharlist[100];
char ch = ' A ';
Char tagchar[100] = {NULL};
CString cStr;
std::string str;

Note: Set locale to output Widechar
SetLocale (Lc_all, "CHS");

Note: char* conversion wchar_t*
Note: wchar_t is not overloaded <<, so cout << output is not available
Pwidechar = Ansitounicode (Pchar);
Note: printf ("%ls") and wprintf (L "%s") are consistent
printf ("%ls/n", Pwidechar);

Note: wchar_t* conversion wchar_t[]
wcscpy (Tagwidecharlist, Pwidechar);
wprintf (L "%s/n", tagwidecharlist);

Note: wchar_t[] Convert wchar_t*
Pwidechar = tagwidecharlist;
wprintf (L "%s/n", Pwidechar);

Note: Char conversion string
Str.insert (Str.begin (), ch);
cout << str << Endl;

Note: wchar_t* conversion string
Pwidechar = new Wchar_t[str.length ()];
swprintf (Pwidechar, L "%s", Str.c_str ());
wprintf (L "%s/n", Pwidechar);

Note: string conversion char*
Pchar = const_cast<char*> (Str.c_str ());
cout << pchar << Endl;

Note: char* conversion string
str = std::string (PCHAR);
Note: cout << overloaded string, if printf must printf ("%s", Str.c_str ());
But not print ("%s", str); Because STR is a string class
cout << str << Endl;

Note: string conversion char[]
str = "boring, boring";
strcpy (Tagchar, Str.c_str ());
printf ("%s/n", Tagchar);

Note: string conversion CString;
CSTR = Str.c_str ();

Note: CString conversion string
str = string (Cstr.getbuffer (Cstr.getlength ()));

Note: char* conversion CString
CSTR = Pchar;

Note: CString conversion char*
Pchar = Cstr.getbuffer (Cstr.getlength ());

Note: CString conversion char[]
strncpy (Tagchar, (LPCTSTR) CString, sizeof (Tagchar));

Note: CString conversion wchar_t*
Pwidechar = Cstr.allocsysstring ();
printf ("%ls/n", Pwidechar);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.