How to convert a Unicode string to \uXXXX? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
xin.songtao
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Apr 18, 2007 6:10 am
Location: Shanghai China

How to convert a Unicode string to \uXXXX?

Post by xin.songtao »

hi,guys,please give me some suggestions.
I want to convert a unicode string to \uXXXX ,

like this:

UTF-8 L"中文" convert to is "\uXXXX\uXXXX"(e.g maybe the result is \u4E2D\u6587);

How to convert? please give me some suggestions.


THanks!
xin.songtao
Experienced Solver
Experienced Solver
Posts: 86
Joined: Wed Apr 18, 2007 6:10 am
Location: Shanghai China

Re: How to convert a Unicode string to \uXXXX?

Post by xin.songtao »

xin.songtao wrote:hi,guys,please give me some suggestions.
I want to convert a unicode string to \uXXXX ,

like this:

UTF-8 L"中文" convert to is "\uXXXX\uXXXX"(e.g maybe the result is \u4E2D\u6587);

How to convert? please give me some suggestions.


THanks!
I have solve it!

Code: Select all

wxString GB2UnicodeEncoding(const wxString &str)
{
	size_t len= str.Len();
	const wxChar* wchar = str.wc_str();

	wxString deststr;
	while(len)
	{
		if (*wchar>127)
		{	
			deststr = deststr + wxString::Format(_T("%s%s%x"),_T("\"),_T("u"),*wchar);
		}
		else
		{
			deststr = deststr + wxString(*wchar);
		}
		wchar++;
		len--;
	}
	return deststr;
}

E.G:
const wxString str = _T("中文test");
after GB2UnicodeEncoding(str);
the result is : \u4E2D\u6587test
Post Reply