Is GetStringChars null terminated?

953 views
Skip to first unread message

ezmora

unread,
Mar 22, 2011, 2:14:15 PM3/22/11
to android-ndk
I am passing a String from Java to the C layer, and call
GetStringChars, in my C code, to obtain a pointer to this String.
I tried printing out this content of this pointer, but I receive
strange results.
Suppose my Java String was "zmora", my C code is:

jchar* uName = (*env)->GetStringChars(env, str, &isCopy);
__android_log_print(ANDROID_LOG_INFO, "TEST", " %s", uName );


The output is simply: "z"
However when I print out uName chars one by one I see that they
contain the correct values:
__android_log_print(ANDROID_LOG_INFO, "TEST", " %c %c %c", uName[0],
uName[1], uName[2] );

Any ideas why this is the case? Does GetStringChars return a Null
terminated buffer?

Igor R

unread,
Mar 22, 2011, 4:37:20 PM3/22/11
to andro...@googlegroups.com

fadden

unread,
Mar 22, 2011, 5:50:58 PM3/22/11
to android-ndk
On Mar 22, 11:14 am, ezmora <eyalzm...@gmail.com> wrote:
> I am passing a String from Java to the C layer, and call
> GetStringChars, in my C code, to obtain a pointer to this String.
> I tried printing out this content of this pointer, but I receive
> strange results.

GetStringChars returns UTF-16 data. It is not null-terminated.

If you want something you can hand to a log call or printf, use
GetStringUTFChars instead.

(And don't forget to Release when done, whichever you use.)
Reply all
Reply to author
Forward
0 new messages