how to communicate the integer value of char from C++ to Java
Well, I also used the Java Programming forum for this question,
but since nobody answered, I am using another forum. What's
really the right forum for this?
I am calling a Java native executable from a C++ program.
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.
But when I recieve the char ch in Java, neither
int i = ((int)(ch)>>8) & 255
nor
int i = (int)(ch) & 255
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?
Thanks for looking at this!
# 1
char in C++ are equivalent to byte variables in java, char variables are UNICODE, equivalanet to w_char in C++.(incidentally this belongs on a JNI forum.
# 2
Thanks! I now moved this question to the JINI forum: http://forum.java.sun.com/thread.jspa?threadID=5174881
# 3
> (incidentally this belongs on a JNI forum.Why? It's really about java native types and/or DataInputStream as far as I can see, and I can't see anything about JNI anywhere.
ejpa at 2007-7-12 10:18:06 >

# 4
Ok, so which forum does my question about how to communicate something from C++ to Java belong to?
# 5
I'm happy with this one. Use DataInputStream and DataOutputStream(), and have a look at the Java Language Specification to see how many bits are in each Java datatype.
ejpa at 2007-7-12 10:18:06 >

# 6
Ok, so I moved to this forum again, since I was told, my question doesn't belong to the JNI forum.
First of all, I do not even really understand the char type in Java.
I was hoping that for all hexadecimal numbers 0x... between 0x0000 and 0xffff there exists a character '\u...'
However, I noticed that for example there is no character
'\u000a'
and there is no character
'\u000d'
When I try to write these characters in Borland JBuilder 2006, I get the message:
illegal line end in character literal.
I am trying to communicate arbitrary numbers between 0 and 255, which are specified as unsigned chars in C++ to Java, by first making chars out of them. But it seems, I wouldn't even be able to communicate the number 10 (0x000a) or 13 (0x000d).
So it doesn't look like that the value of the C++ char, which lies between -128 and 127, is communicated to the same value of the Java char of my Java string.
Is it possible to completely abandon using Java characters and to make a file that can be converted to an array of single bytes (not two bytes like characters or more bytes)?
Thanks for your time!
# 7
How do you want to C+ and the Java programs to communicate? Through a file? a pipe? a socket?
# 8
Through a file or a socket.
# 9
I am presently trying to communicate with a file.
# 10
>
> So it doesn't look like that the value of the C++
> char, which lies between -128 and 127, is
> communicated to the same value of the Java char of my
> Java string.
>
As I told you in the other thread all that matters is the bits, not the display value.
And in C++ there is no such thing as a 'char' value with a value less than zero. That is because there is no charset in the world that has negative values. So however you are displaying it in C++ is wrong as well (and display again has nothing to do with the bits of the value.)
Also as I suggested in the other thread you should be using hex and not integer values.
> Is it possible to completely abandon using Java
> characters and to make a file that can be converted
> to an array of single bytes (not two bytes like
> characters or more bytes)?
Yes.
# 11
Ok, so how do I write the char as hex in C++ and how do I read it as hex in Java?
How can I communicate just hex bytes without even using chars?
(I know, in Java characters never have negative value. but why can't they have all positive values? And in C++ why is there the char type ranging from -128 to 127 as opposed to the unsigned char type ranging from 0 to 255, if there is no char with a negative value? My understanding is that the char type in C++ is the same as the byte type in Java, am I correct?)
Well, sorry, I am a mathematician, not really a programmer, even though I have programmed for 25 years.
Thanks for your patience with me!
# 12
> Ok, so how do I write the char as hex in C++ and how
> do I read it as hex in Java?
When you display it you should use hex in both languages. Presumably you are doing that to determine that the values come across correctly.
Presuming that you haven't mangled the values in C then you just write them as chars (C 8 bit values.) In java you read them as bytes.
If you need to construct a 16 bit value then you will have to combine them.
> (I know, in Java characters never have negative
> value. but why can't they have all positive values?
Still not the point.
You are moving bits. All you should care about is the bits.
However you need to correctly display them and understand that there is a difference between a display value and the bits.
> And in C++ why is there the char type ranging from
> -128 to 127 as opposed to the unsigned char type
> ranging from 0 to 255, if there is no char with a
> negative value? My understanding is that the char
> type in C++ is the same as the byte type in Java, am
> I correct?)
>
It is a data type, not a character set.
But regardless you should only be concerned with the bits in each and not the type.
> Well, sorry, I am a mathematician, not really a
> programmer, even though I have programmed for 25
> years.
Basically in both language 8 bits will end up as unsigned values when you do the following (because the following excludes sign extension if you care.)
byteValue & 0x0ff
Notice the the above doesn't change the value (contents) of byteValue. All it does is allow you to display the bits in a non-negative form.
# 13
Ok, so how exactly is the reading of bytes done in Java?Should I use javax.jnlp (FileOpenService, JNLPRandomAccessFile, etc)?And how exactly is this done?Sorry, I am a beginner at this.Thanks a lot in advance!
# 14
Well, it seems, I shouldn't use FileOpenService, since I want the program to open the file, not the user. So how does my Java program open the file and get the byte array?
# 15
http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInputStream.html#readByte()or indeed any of its read() methods.
ejpa at 2007-7-21 21:17:43 >

# 16
I found an answer to the previous question about how to communicate the integer value of an unsigned char from C++ to Java. The unsigned char array contains the r, g, b values of an image. Here is the answer, I had to receive the chars as bytes:
In Java I wrote the code:
int[] pix = new int[count]; //each element has the r, g, b values
//count = width*height
FileInputStream pixelFile = new FileInputStream("C:\\pixelvalues.dat");
DataInputStream pixelData = new DataInputStream(pixelFile);
byte[] pixelByte = new byte [3*count];
try
{
pixelData.read(pixelByte);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
red = (int)pixelByte[y*pixelsw + x] & 0xff;
green = (int)pixelByte[count + y*pixelsw + x] & 0xff;
blue = (int)pixelByte[2*count + y*pixelsw + x] & 0xff;
pix[y*pixelsw + x] = 0xff000000 | ((red<<16) + (green<<8) + blue);
}
}
}
catch (IOException e)
{
}
and in C++ I wrote the code:
unsigned char *pixels = ... //the length of this array is 3*count
const char *charpixels = reinterpret_cast<char*>(pixels);
fstream file;
file.open("C:\\pixelvalues.dat", ios::out | ios::binary);
file.write(charpixels, 3*count);
file.close();
