开发者

Signed und Unsigned Integers in Preon

I want to use Preon for project that communicates with a server written in C. The protocol depends on the native endianess of the machine (you can solve with thisjava.nio.ByteOrder.getNative() under the assumption that the JVM has the same endianess as the server) and uses uint64_t for data lenghts and int32_t for status c开发者_如何学运维odes (a negative value indicates an error).

I couldn't find information about signedness in the Preon documentation. I had a quick look at the source code and found that nl.flotsam.preon.buffer.DefaultBitBuffer uses signed shifts (<< and >>) and the javadoc comments of several methods in nl.flotsam.preon.buffer.BitBuffer also indicate that it uses signed integers, but the javadoc comment nl.flotsam.preon.buffer.BitBuffer says that BitBuffer uses unsigned integers. This confused me.

What is the default integer format of Preon? How do I represent uint64_t and int32_t?


For data marked with @BoundNumber, the default is LittleEndian. You can override that like this:

@BoundNumber(byteOrder=BigEndian)

… but platform specific currently is not an option. That is a limitation. You might want to consider filing an issue for it. (Otherwise, I will, somewhere in the future.)

The way I could see it work is to just have another enum constant, called Platform.

Update:

For byte order, Preon now (currently only available in the head) has three options: BigEndian, LittleEndian and Native. Native will resolve in the byte order indicated by java.nio.ByteOrder.getNative().

As a consequence, it is now possible to write code like this:

@BoundNumber(byteOrder=Native) int value;

… which translates into a signed 32 bit integer, with byte order based on the architecture.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜