how to communicate numbers from Java to C++
I am calling a Java executable from a C++ program.
I need to communicate some ints and doubles from Java to C++.
I am trying to make a file of them and in C++ I wait until the file has been made.
However if I convert them to strings and make a file of chars, the problem is that in Java chars are 2 bytes and in C++ they are 1 byte.
What's the best way to make this communication?
# 1
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
# 2
Thanks for your quick answer, but I do no longer want to use JNI.
I am calling a Java native executable, and everything works, like some communication from C++ to Java, which I also accomplish with a file.
I just need to finish my communication of numbers from Java to C++ with a file.
Thanks for your time!
# 3
And how exactly is this question different than your previous question? http://forum.java.sun.com/thread.jspa?threadID=5174857&start=0&tstart=0
# 4
Well, I actually 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();
You are right for communicating in the backwards direction from Java to C++, I probably need to do the same thing, I have to use bytes in Java instead of chars.
# 5
The problem is when I have a char ch,(byte)ch or (int)chdoes not give the integer value of ch,because it gives 0 for '.' instead of 46,0 for 'x' instead of 120and 0 for ' ' instead of 32.How can I find the correct integer value of a
# 6
If you're talking about Java to C++ you shouldn't be sending chars at all, only bytes.
ejpa at 2007-7-12 16:38:47 >

# 7
> The problem is when I have a char ch,
> (byte)ch or (int)ch
> does not give the integer value of ch,
> because it gives 0 for '.' instead of 46,
> 0 for 'x' instead of 120
> and 0 for ' ' instead of 32.
>
> How can I find the correct integer value of a
> character?
You start by displaying hex, not integers. Which I stated several times in the other thread.
# 8
Well, if I make a String out of the char ch, and then use getBytes:
char[] chars = new char[1];
chars[0] = ch;
String str = new String(chars);
byte[] bytes = stri.getBytes();
then bytes[0] gives me the correct integer value of ch, as long as the integer value of ch is <= 127, which is all I need.
This makes it possible to communicate ch to C++.
But how would I use hex values, since I am not explicitely defining the char ch?
# 9
Bytes get mapped to chars, which means the value can change. So hopefully
1. The bytes are actually supposed to be chars.
2. You are using the correct encoding.
Other than that you use the functionality of printf() in C to print hex values. And in java you use the methods of the Integer class.