Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to convert the value of an unsigned char in C++ to Java

843829May 21 2007 — edited May 22 2007
Well, I also used the Networking forum for this question,
but I was told it belongs to the JNI forum.

I would like to communicate the red, green, and blue values of an image, which are unsigned chars in my C++ program (varying between 0 and 255) to my Java program.

I first convert the unsigned chars in my C++ program to chars.

Unfortunately, chars in C++ are 1 byte and chars in Java are 2 bytes and I might have to reverse the order of bytes. However, at least they are unsigned.

But when I recieve the char ch in Java, neither
int i = (int)(ch)>>8 (looking at the first byte)
nor
int i = (int)(ch)
seems to give the same value as the value of the unsigned char in C++.

What's a correct and fast way to make this communication?

Can I receive the char as a byte by in Java, and use
int i = ((int)by) & 255?

Thanks for looking at this!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 19 2007
Added on May 21 2007
7 comments
259 views