Page 1 of 1

wxChar to utf8 char

Posted: Mon Mar 09, 2009 9:33 am
by lester
Please help, how to convert wxChar to character in UTF8( int )?

Posted: Mon Mar 09, 2009 11:47 am
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;
	}
...