A c # binary file generation method that complies with Java rules,

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.