wxChar to utf8 char 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
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

wxChar to utf8 char

Post by lester »

Please help, how to convert wxChar to character in UTF8( int )?
lester
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Sep 02, 2006 7:24 pm
Location: Ukraine

Post by lester »

Solution for me:

Code: Select all

/**********************************************************************************************/
int UtfToUniChar( char *str, int* chPtr )
{
	int byte = *((unsigned char *) str);
	if( byte < 0xC0 )
	{
		*chPtr = (int) byte;
		return 1;
	}
	else if( byte < 0xE0 )
	{
		if( ( str[ 1 ] & 0xC0 ) == 0x80 )
		{
			*chPtr = (int) (((byte & 0x1F) << 6) | (str[1] & 0x3F));
			return 2;
		}

		*chPtr = (int) byte;
		return 1;
	}
	else if( byte < 0xF0 )
	{
		if( ( ( str[ 1 ] & 0xC0 ) == 0x80 ) && ( ( str[ 2 ] & 0xC0 ) == 0x80 ) )
		{
			*chPtr = (int) ( ( ( byte & 0x0F ) << 12 ) |
				( ( str[ 1 ] & 0x3F ) << 6 ) | ( str[ 2 ] & 0x3F ) );

			return 3;
		}

		*chPtr = (int) byte;
		return 1;
	}

	*chPtr = (int) byte;
	return 1;
}
...
	char ustring[ 32768 ];
	strcpy( ustring, (const char*) text.mb_str( wxConvUTF8 ) );

	char* next = ustring;
	for( int i = 0 ; *next ; ++i )
	{
		int ch = 0;
		int len = UtfToUniChar( next, &ch );
		next += len;
	}
...
Post Reply