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!

[932 byte] By [BrigitAnanyaa] at [2007-11-27 5:01:21]
# 1
Sounds overly-complicated to me. Why don't you just return the values as (positive) ints?
bschauwejavaa at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
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!
BrigitAnanyaa at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Hi,Do you launch a new process for each :- image ?- pixel ?--Marc ( http://jnative.sf.net)
mdentya at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 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.

jschella at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
I launch a new process for each image. I actually close my Java program before I get a new image when calling the Java executable again.
BrigitAnanyaa at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Well, now I was told that my question really doesn't belong to the JNI forum, so I moved it back to the Network forum: http://forum.java.sun.com/post!reply.jspa?messageID=9675728Please check my most recent message at the link above.Thanks!
BrigitAnanyaa at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
Ouuups, sorry, the correct link to my question at the Networking forum is: http://forum.java.sun.com/thread.jspa?threadID=5174857&tstart=0
BrigitAnanyaa at 2007-7-12 10:18:41 > top of Java-index,Java HotSpot Virtual Machine,Specifications...