Home > Back-end >  The same single character and wide character conversion of C code, VC under normal and abnormal unde
The same single character and wide character conversion of C code, VC under normal and abnormal unde

Time:02-27

On the QT source:
 
#include
#include
#include
using namespace std;

STD: : string ws2s (const STD: : wstring & amp; Ws)
{
Size_t I;
STD: : string curLocale=setlocale (LC_ALL, NULL);
The setlocale (LC_ALL, "");
Const would * _source=ws. C_str ();
Size_t _dsize=2 * ws. The size () + 1;
Char * _dest=new char [_dsize];
Memset (_dest, 0 x0, _dsize);
Wcstombs_s (& amp; I, _dest, _dsize _source, _dsize);
STD: : string result=_dest;
The delete [] _dest;
Setlocale (LC_ALL, curLocale c_str ());
return result;
}

STD: : wstring s2ws (const STD: : string & amp; S)
{
Size_t I;
STD: : string curLocale=setlocale (LC_ALL, NULL);
The setlocale (LC_ALL, "CHS");
Const char * _source=s.c _str ();
Size_t _dsize=s.s considering () + 1;
Would be * _dest=new would [_dsize];
Wmemset (_dest, 0 x0, _dsize);
Mbstowcs_s (& amp; I, _dest, _dsize _source, _dsize);
STD: : wstring result=_dest;
The delete [] _dest;
Setlocale (LC_ALL, curLocale c_str ());
return result;
}


Int main (int arg c, char * argv [])
{
QCoreApplication a (arg c, argv);
STD: : string strTest="1234 Chinese abcd";
STD: : wstring destWStr=s2ws (strTest);
Return a.e xec ();
}

Execution:



On VS source:
 
#include
#include
#include
using namespace std;

# pragma warning (4996) disable:

STD: : string ws2s (const STD: : wstring & amp; Ws)
{
Size_t I;
STD: : string curLocale=setlocale (LC_ALL, NULL);
The setlocale (LC_ALL, "");
Const would * _source=ws. C_str ();
Size_t _dsize=2 * ws. The size () + 1;
Char * _dest=new char [_dsize];
Memset (_dest, 0 x0, _dsize);
Wcstombs_s (& amp; I, _dest, _dsize _source, _dsize);
STD: : string result=_dest;
The delete [] _dest;
Setlocale (LC_ALL, curLocale c_str ());
return result;
}

STD: : wstring s2ws (const STD: : string & amp; S)
{
Size_t I;
STD: : string curLocale=setlocale (LC_ALL, NULL);
The setlocale (LC_ALL, "");
Const char * _source=s.c _str ();
Size_t _dsize=s.s considering () + 1;
Would be * _dest=new would [_dsize];
Wmemset (_dest, 0 x0, _dsize);
Mbstowcs_s (& amp; I, _dest, _dsize _source, _dsize);
STD: : wstring result=_dest;
The delete [] _dest;
Setlocale (LC_ALL, curLocale c_str ());
return result;
}

Int main ()
{
STD: : string strTest="1234 Chinese abcd";
STD: : wstring destWStr=s2ws (strTest);
return 0;
}


VS execution results:

CodePudding user response:

For computer no noise, only the binary bytes; To the human brain is gibberish, GBK: 0 xb0 0 xa1, Unicode - 16 LE: 0 x4a 0 x55, Unicode - 16 BE: 0 x55 0 x4a, utf-8 8-0 x95 xE5 0 0 x8a

CodePudding user response:

Qt compiler MSVC code you tried no problem don't directly in the code to read from the file in Chinese try it could be your Qt Creator configuration problem

CodePudding user response:

refer to the second floor mango black response:
Qt compiler MSVC code you tried no problem don't directly in the code to read from the file in Chinese try it could be your Qt Creator configuration problem


I feel is a problem of encoding format, but where is the change is not too clear,

CodePudding user response:

reference sty_app reply: 3/f
Quote: refer to the second floor mango black response:
Qt compiler MSVC code you tried no problem don't directly in the code to read from the file in Chinese try it could be your Qt Creator configuration problem


I feel is a problem of encoding format, but where is the change is not too clear,

The source code, save as a character encoding, choose you want...
  • Related