Showing chinese character

Hi

The char * str below contain sequence of UTF-8 code units, for the 2 chinese character 你好. The message box cannot display the 2 chinese character correctly, any ideas why ? I am using Visual C++.

1
2
3
4
5
6
	char * str = "\xE4\xBD\xA0\xE5\xA5\xBD";	
	size_t len = strlen(str) + 1;
	wchar_t * wstr = new wchar_t[len];
	size_t convertedSize  = 0;
	mbstowcs_s(&convertedSize, wstr, len, str, _TRUNCATE);
	MessageBoxW( NULL, wstr, (LPCWSTR)L"Hello", MB_OK);



Thanks
Last edited on
I don't think UTF-8 characters can hold Chinese characters.
How did you get those values?

You may need to directly use wchar_t* instead of char*.
Hi Thanks for your reply

I got this value from http://rishida.net/tools/conversion/ in the textbox of UTF-8 code units

The code that I have is a simulation to my problem. How can I represent the 2 chinese character in the char * str correctly ? I am modifying a source code which limit the datatype of str to be only char *. But the content can be anything I want.
Last edited on
We can still put wchar_t raw content into the char string, but this requires a cast.

Check if this works out for you:

1
2
	char * str = "\xE4\xBD\xA0\xE5\xA5\xBD\0\0"; // Double Terminator!
	MessageBoxW( NULL, (LPCWSTR)str, (LPCWSTR)L"Hello", MB_OK);


EDIT - Does not seem to be working anyways. I'm still looking into it.
RE-EDIT: If you expect to put mixed wchar_t* and char* content into your string, you should use wchar_t* only and convert char* to wchar_t* via mbstowcs_s.
But I think for this kind of things you should use wchar_t*'s.
Last edited on
Hi thanks for your help.

I am using the function with the locale, now the character look little bit better, but still incorrect, look like Japanese characters are shown instead. Maybe the locale is not correct ?

1
2
3
4
5
6
7
8
9
	//printer test 
	char * str = "\xE4\xBD\xA0\xE5\xA5\xBD";	
	size_t len = strlen(str) + 1;
	wchar_t * wstr = new wchar_t[len];
	size_t convertedSize  = 0;

	_locale_t local = _create_locale( LC_ALL , "Chinese");
	_mbstowcs_s_l(&convertedSize, wstr, len, str, _TRUNCATE, local);
	MessageBoxW( NULL, wstr , (LPCWSTR)L"Hello", MB_OK);
Last edited on
I'm sorry I really cannot help you with this.
Your example still shows empty cubes, sign that my OS won't draw chinese/japanese characters.
But, try using "chs" instead of "Chinese", even if that shouldn't be the issue...
RE-EDIT: If you expect to put mixed wchar_t* and char* content into your string, you should use wchar_t* only and convert char* to wchar_t* via mbstowcs_s.
But I think for this kind of things you should use wchar_t*'s.


I know I can just do, then problem solved.
1
2
wchar_t * wstr = L"你好";
MessageBoxW( NULL, wstr , (LPCWSTR)L"Hello", MB_OK);


But I am simulating a situation which I cannot just use wchar_t*s directly, I have third party source code that provide me char * str, and I have to convert this to wchar_t*

Thanks
Last edited on
Wait, you provide a third party source code a char*, to get back (in another place) the same char*?

In this case you can simply:
1
2
3
4
5
char * str = (char*)(L"你好");
// Provide "str" to this third party source code
// Once you get again "str", now you can:
wchar_t * wstr = (wchar_t*)(str);
MessageBoxW(NULL, wstr, (LPCWSTR)L"Hello", MB_OK);
Hi,

yes, that's exact of the scenario. For the message box now it works. But for that third party source still printing incorrectly. I am actually correcting the PHP C source code for php_printer.dll module.


Hi

This is the PHP source code that I tried modify. Which still haven't got it right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PHP_FUNCTION(printer_draw_text)
{
	zval **arg1, **arg2, **arg3, **arg4; 
	printer *resource;
	LPCWSTR str;
	wchar_t * wstr2;

	if( zend_get_parameters_ex(4, &arg1, &arg2, &arg3, &arg4) == FAILURE ) {
		WRONG_PARAM_COUNT;
	}

	ZEND_FETCH_RESOURCE(resource, printer *, arg1, -1, "Printer Handle", le_printer);

	convert_to_string_ex(arg2);
	convert_to_long_ex(arg3);
	convert_to_long_ex(arg4);

	wstr2 = (wchar_t*)(Z_STRVAL_PP(arg2)); //arg2 is the 你好 string that I pass to

	//ExtTextOut(resource->dc, Z_LVAL_PP(arg3), Z_LVAL_PP(arg4), ETO_OPAQUE, NULL, Z_STRVAL_PP(arg2), Z_STRLEN_PP(arg2), NULL); //This is the original line
	ExtTextOutW(resource->dc, Z_LVAL_PP(arg3), Z_LVAL_PP(arg4), ETO_OPAQUE, NULL, wstr2 , wcslen (wstr2), NULL); //This is my version

}
Last edited on
Try to use void MakeUnicodeString and MakeAnsiString from this artice:
http://strongcpp.blogspot.ru/2013/04/ansi-unicode.html
Topic archived. No new replies allowed.