Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to convert jchararray to char array.

 
bharani rao
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[]:
 
bharani rao
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Rob..

very much
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
bharani rao
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you rob...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic