• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why isn't this char to jchar working?

 
Dan Bizman
Ranch Hand
Posts: 387
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really is an array/vector of chars, not a string (i.e. not a char* string).



It returns garble to Java instead of the characters put into the vector. What am I doing wrong?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

jchar is a 16-bit type, like Java char. By (effectively) copying an array of char into an array of jchar, you're packing two chars into one jchar for half the array, then ignoring the other half. You'd have to have an array of wchar_t to copy in this way.

I don't know what the best way to do this correctly is; I've never thought about this case before. You could do a loop of course and set one element at a time; you could allocate a twice-as-large buffer in C, "scatter" the chars into it, then use the above code. You could even create a Java String from your chars, ask *it* for the char array, and return that.
 
Dan Bizman
Ranch Hand
Posts: 387
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Hi,

jchar is a 16-bit type, like Java char. By (effectively) copying an array of char into an array of jchar, you're packing two chars into one jchar for half the array, then ignoring the other half. You'd have to have an array of wchar_t to copy in this way.

I don't know what the best way to do this correctly is; I've never thought about this case before. You could do a loop of course and set one element at a time; you could allocate a twice-as-large buffer in C, "scatter" the chars into it, then use the above code. You could even create a Java String from your chars, ask *it* for the char array, and return that.


Thanks. Yeah, I ended up going with the last: creating a jstring and then getting it's characters as an array from the JNIEnv.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic