Problem when Java and C ++ are talking to each other

Hi guys, I have a C ++ program and it writes a binary file to disk. Then I use a Java program to read the number. The problem is the number read is different from the number written ... Let's say I write the integer 4 using C ++ and return 67108864 when using JAVA to read it (using readint ()) ... I suspect this is due with a large or small end. Do you have any simple solutions to solve this problem?

Thanks a lot!

+4
source share
1 answer

Java java.nio buffers let you specify content.

See http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html , especially the order method, which allows you to specify endianness and the getInt method, which allows you to read int.

To read a file using ByteBuffer , follow these steps:

 ByteBuffer buffer = new RandomAccessFile(myFile, "r") .getChannel.map(MapMode.READ, offset, length); 

Remember to close it when done.

+3
source

Source: https://habr.com/ru/post/1344198/


All Articles