user654628
user654628

Reputation: 1459

Android ndk log unicode (wide characters)

I am trying to write to logcat unicode characters (UTF16; wide characters like Japanese, Korean or Chinese). The only success is a work around which is to send it to Java as a string (for example the code below):

unsigned short* text = new unsigned short[100];      // or jchar*
.... // all some unicode to the array
jstring jtext = jniEnv->NewString(text, (jsize)length);
jniEnv->CallVoidMethod( JavaClass, JavaPrintUnicode, jtext );
... // Clean up

// Then java prints it out

However if the bytes are in a char array, __android_log_print will print garbled text. Is there a method to print out the text within the NDK (C++) side?

Upvotes: 1

Views: 2839

Answers (2)

Alex Cohn
Alex Cohn

Reputation: 57173

Android NDK, as typical for Linux, uses 32 bits to represent wide chars, so if conversion to UTF-32 is trivial, you can try this:

unsigned short text[100];
// fill text with UTF-16; make sure that all characters are 2 bytes long
long utf32[100];
for (int i=0; i<100; i++)
  utf32[i] = text[i];

__android_log_print(ANDROID_LOG_INFO, "UTF-32", "%ls", utf32);

Upvotes: 4

grillSandwich
grillSandwich

Reputation: 107

You can print hex values for each character instead.

void print_string_as_hex(WCHAR* ws){
    int l = wcslen(ws);
    for(int i = 0 ; i < l ; i++) 
        __android_log_print(ANDROID_LOG_INFO, "MyTag", "%x", ws[i]);
}

To get the encoded character from hex values, you can use Microsoft Word - 4513[Alt-x] will encode 0x4513for you.

Upvotes: 0

Related Questions