开发者

library to serialize numbers across programing language and machine [closed]

开发者 https://www.devze.com 2023-01-14 09:02 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 3 years ago.

Improve this question

I have a program in Java that needs to write a file with integers of 8, 16, 32 and 64 bits and floating point numbers of 16, 32 and 64 bits. This file needs to be read by a program in C (and vice-versa). These programs are used in different type of machines, so the file needs to be independent of little or big endian.

I am looking for a library in Java and in C for doing this. The library needs to be compact, because this will be used also in a micro-controller.

note: At the moment, I am implementing the functions myself, and it works fine (so, no need to give me an ad-hoc solution). note: I am aware that I could write the numbers in a human readable format, and use scanf to read them; I pr开发者_如何转开发efer not to use this solution now, because it is slower and takes more place.


Google's Protocol Buffers seem like what you're after.


On the Java side, you can just use DataOutputStream, it's guaranteed to use big endian format aka network byte order. On the C side, you can use the ntohl() and ntohs() functions to convert network byte order to host byte order.


One possibility would be to use Sun's XDR format. Oddly, I don't believe Java ships with support for it, but if you look around, it's easy to find. Sun does include an XDR library with Solaris; if you're not using Solaris, it's easy to find implementations for C as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消