PJChar is defined as a pointer to a word. Casting to WideString isn't
working since internally Widestring has extra bytes for reference count and
string length. These extra bytes are locate before the actual string. This
explains why assigning Pointer(WStr) to PJChar works.
Result := WideString(PWideChar(Chars));
PWideChar matches probably PJChar and the compiler knows how to convert
PWideChar to WideString.
Re: RE : [fpc-pascal] android JNI JString to String
On 26 Apr 2011, at 13:06, herux wrote:
> I debug this line of code using android log
> Chars := Env^.GetStringChars(Env, AJString, nil);
> and the log I retrieved :
> 04-26 17:53:17.942: DEBUG/proj_debug(711): e <--- only "e" character
That is normal, because Chars is probably an array of jchar, and a
jchar is utf-16 (= widechar). That means that if you treat it as pchar
and it only contains plain ASCII, the second "ansichar" will always be
#0 on a little endian system (on a big endian system, the first
"ansichar" will already be #0).
You could also go the hard way, something along the lines of:
Chars := Env^.GetStringChars(Env, JStr, @IsCopy);
if (Chars = nil) then begin
Result := ''
end else begin
Env^.ReleaseStringChars(Env, JStr, Chars);