how to convert the value of an unsigned char in C++ to Java
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!
# 4
> Because I am not really using JNI. I call a Java
> native executable from C++ and communicate the
> picture information either with the args or with a
> file, and it has to be fast, since a picture has a
> lot of pixels.
>
> Please help! Thanks!
That does not answer the question.
Best I can guess is that you have a 16 bit value. Anything else about it is completely irrelavent (postitive or negative int value.)
And you are writing that either to a file or a stream. You most assuredly can't write a normal picture to command line args because command lines have length limits.
In your java application you are then attempting to read that 16 bit value (a lot of 16 bit values)
The only thing that matters is the byte order.
And for verification you should be outputting the value as hex (not integers) in both the C code and the java code.
In java you extract a 16 bit value using the following
int newVal = oldVal & 0x0ffff;
You can shift and or to change from 8 bit to 16 bit values.