A c # binary file generation method that complies with Java rules,
1. Real problems in a project
In the actual project, I wanted to use the C # tool to generate the SHP binary space index file, and then the java server will parse the space index file for use.
In actual use, it is found that the content of the file parsed by java is very different from that written by C #. For example, the double values parsed in java are very large negative numbers.
2. troubleshoot 2.1 test whether the C # write is incorrect
Check that the written value is correctly interpreted in C.
2.2 Encoding Error
Use java to generate the same file. The encoding is the same as that of the C # generated file, but the content is different. Therefore, it is not caused by an Encoding Error.
2.3 conclusion
It is inferred that the reason for the dislocation is that the value written by C # is different from the value written by Java.
3. Cause of error
In a. C #, the byte range is [0,255], while in Java, the byte range is [-128,127].
Byte sorting in B. C # is low-end sorting, but direct sorting in Java is high-end sorting. For example, the double value is stored in java as writes that long value to the underlying output stream as an 8-byte quantity, high byte first.
4. Solution 4.1 description
Use sbyte [-128,127] in C # To put the byte array stored in C # upside down, convert each byte to sbyte, and finally store the entire sbyte array.
4.2 specific implementation code
5. Result Verification
Write in C:
Read in Java:
The results are consistent.
-- Welcome to reprint, but retain the copyright, Please clearly indicate the source: http://www.cnblogs.com/naaoveGIS/