I want to write an integer in a binary file with Java (Android) and then read it with a C++ code. My code in Java is:
byte [] mybuffer = ByteBuffer.allocateDirect(4).putInt(1000).array;
out.write(mybuffer, 0, 4); // out is FileOutputStream
The reader in C++
std::ifstream fileToRead;
fileToRead.open("myFile", std::ios::binary);
if (!fileToRead.is_open()){
std::cout << "[ERROR] Can't open file" << std::endl;
exit(-1);
}
int * myInt = new int;
fileToRead.read((char*)&myInt[0], 4);
std::cout << " The integer is " << myInt[0] << std::endl;
But I get values which doesnt make sense.
Thanks
output Java:
buffer[0] = 0
buffer[1] = 0
buffer[2] = 3
buffer[3] = -24
output c++:
The integer is -402456576