File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes how to convert jchararray to char array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "how to convert jchararray to char array." Watch "how to convert jchararray to char array." New topic
Author

how to convert jchararray to char array.

bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi all,

We have a project which uses JNI.

we are struck up in converting jchar[] to char[]

i have searched this forum.
it produced only 4 results and 3 three of them are convertin char[] to jchar[] ( the reverse process of my requirement)

can anyone tell the procedure...

i have been refering
http://java.sun.com/docs/books/jni/download/jni.pdf with some unclear ideas..

thanks in advance..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

With char[], do you mean native char[]? If so, you will have a challenge - jchar is usually twice as big as (native) char, which is more compatible with Java's byte (jbyte).

Of course the quick and dirty solution that only works for (extended?) ASCII is just casting the jchar to a (native) char, cutting off the extra byte. But like I said, it only works if that byte is actually 0, or you will loose data.

If it's a full Unicode jchar[], then I wish you luck. You will have to encode the jchar[] to (native) char[], and that's not an easy job. Well, not from native code. You can do it in Java using the java.nio.charset package. So perhaps that is a better option - converting the (Java) char[] to (Java) byte[] using java.nio.charset, then use that (Java) byte[] in native code. It will turn into a jbyte[], but jbyte and (native) char are compatible.


Code for converting a (Java) char[] to a (Java) byte[]:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
thank you very very much Rob...

the char[] i meant was char array. hope what i meant and what you got was right!!


thank you very much once again
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
rob,

did the code given by you is used to convert jchar[] ( char array in jni ) to normal return type char[] ???


thank you
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

That's Java code, to convert a jchar[] into a jbyte[]. You should then pass the jbyte[] to the native code, because converting a jbyte[] to a char[] is quite easy. In pseudo code:
This does not make chars a string though.
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
thank you Rob..

very much
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You're welcome.
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi rob,

what you said yesterday was helpful..
but,

let me make the question clear....

we have native char array in c++ ( or c ).
we are passing the a jchar from a java program using JNI.

now we want to pass the native char array value to the jchar array ( jchar, i suppose it comes from JNI).

is it possible.. if possible, how??
do not think i'm asking too many simple questions...
i'm relatively new to java.

thank you in advance
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

A jchar (2 bytes) can contain a char (1 byte), so just casting* should be enough.

* Each separate element, not the entire array
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
thank you rob...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to convert jchararray to char array.