passing utf strings to java from c in jni??  
Author Message
Elhanan





PostPosted: 2006-3-10 1:12:00 Top

java-programmer, passing utf strings to java from c in jni?? hi...

i've managed to far to pass a string to java from c..

but now i wanna pass a hebrew string, and as i hardcode it in c, i get
question marks on the java side.

 
Roedy Green





PostPosted: 2006-3-10 1:58:00 Top

java-programmer >> passing utf strings to java from c in jni?? On 9 Mar 2006 09:11:30 -0800, "Elhanan" <email***@***.com> wrote,
quoted or indirectly quoted someone who said :

>but now i wanna pass a hebrew string, and as i hardcode it in c, i get
>question marks on the java side.

Try going the other way you know what you are aiming for. It will be
16-bit not 8-bit as it likely it is on the C side. If you can only
construct 8-bits on the C side, you will have to decode it on the Java
side, perhaps with Cp424, Cp862, Cp1255, ISO-8859-8, MacHebrew or
windows-1255.

See http://mindprod.com/jgloss/encoding.html
for code.


mindprod.com will be down until 10:15 PST estimated. Somebody cut a
fibre optic cable taking out a good section of downtown.

Think of the havoc a team of modern terrorists could cause with a
manhole lifter and a pair of scissors.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Thomas Fritsch





PostPosted: 2006-3-10 2:31:00 Top

java-programmer >> passing utf strings to java from c in jni?? Roedy Green wrote:
> On 9 Mar 2006 09:11:30 -0800, "Elhanan" <email***@***.com> wrote,
> quoted or indirectly quoted someone who said :
>
>>but now i wanna pass a hebrew string, and as i hardcode it in c, i get
>>question marks on the java side.
>
> Try going the other way you know what you are aiming for. It will be
> 16-bit not 8-bit as it likely it is on the C side.
I would second that.

Working with 16-bit Unicode values in JNI is easy, because you are
familiar with these values from the Java side:
jchar unicode[] = { 0x05d0, 0x05d1, 0x05d2 }; // aleph, beth, gimel
jstring = env->NewString(unicode,3);

But working with 8-bit UTF8 in JNI is difficult and error-prone:
char utf8[] = { 0xD7,0x90,0xD7,0x91,0xD7,0x92 }; // aleph, beth, gimel
jstring = env->NewStringUTF(utf8, 6);

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
 
 
Chris Uppal





PostPosted: 2006-3-10 17:02:00 Top

java-programmer >> passing utf strings to java from c in jni?? Elhanan wrote:

> i've managed to far to pass a string to java from c..

Congrats !


> but now i wanna pass a hebrew string, and as i hardcode it in c, i get
> question marks on the java side.

If you are on Windows and the character data is in the form of correct wstrings
(16-bit) then that's the same format (less the null-byte) as is expected by
NewString() (NB: not NewStringUTF()) so that might be easier to use. Other
platforms will presumably have equivalents, what you are looking for is a way
to format the data as UTF-16.

But if that's not available, or you have to use NewStringUTF(), then you'll
have to consider two things, and then craft your C code to produce the correct
data at the byte-level.

First work out what the Unicode representation of your Hebrew text would be,
i.e the sequence of Unicode code points corresponding to that text. Then look
at the JNI documentation where the mapping between that and actual bytes is
carefully speficied. Use that to generate a corresponding sequence of bytes.
Each Unicode character will translate to between 1 and 6 bytes.

Actually, you may be able to take a short-cut. If your text is all in Unicode
characters which are in the 16-bit range EXCLUDING 0, then the JNI format is
identical to real UTF-8. You could use the system-supplied UTF-8 encoding.
But if you do, be very careful to document the fact that you are using a
potentially fragile hack.

-- chris


 
 
Elhanan





PostPosted: 2006-3-10 17:59:00 Top

java-programmer >> passing utf strings to java from c in jni?? actually this dll is to be used as a webservice in biztalk so i nave no
idea how the strings will actually pass, right now i'm just trying to
hardcode these things for a test.


maybe i'll wrap it up in a dotnet dll.. don't know how is that going to
effect everything..

 
 
Chris Uppal





PostPosted: 2006-3-10 18:18:00 Top

java-programmer >> passing utf strings to java from c in jni?? Elhanan wrote:

> actually this dll is to be used as a webservice in biztalk so i nave no
> idea how the strings will actually pass, right now i'm just trying to
> hardcode these things for a test.

I think it's a good bet that MS will be using wide strings by default.

Rather off-topic, but I advise you to add some tracing to see when the DLL is
loaded and unloaded. I think that /if/ the DLL is unloaded then reloaded, and
if that causes you to shutdown the JVM, you won't be able to restart the JVM in
the second invocation.

I don't know how BizTalk handles DLLs, so this may not be a problem. But it's
worth the warning. At least, unless you've worked out a way to make the JVM
restart -- in which case I'd like to know it too, please !

-- chris


 
 
Elhanan





PostPosted: 2006-3-12 4:45:00 Top

java-programmer >> passing utf strings to java from c in jni?? do you mean the bug i've read in sun's forum about the jvm in a
process, i thought it was closed?

meanwhile my c portion hangs at (*jvm)->DestroyJavaVM(jvm) even for
the first time, this only happens if the java side calls an rmi server,
i wonder if there is a connection between the two.