简体   繁体   中英

How to write Numeric Unsigned, four bytes integer using the Apache Commons libs?

I have to write in a buffer some integer values. But in the API is specified that the number is Integer, Numeric Unsigned and must have at maximum 4 bytes .

How to write Numeric Unsigned, four bytes integer using the Apache Commons libs?

You can do this a number of ways using the built in libraries, so I don't know how you are intenting to use Apache Commons.

DataOutputStream dos = ...
out.writeInt((int) unsignedIntValue);

similarly

ByteBuffer bb = ...
bb.putInt((int) unsignedIntValue);

or for little endian format

ByteBuffer bb = ... .order(ByteOrder.LITTLE_ENDIAN);
bb.putInt((int) unsignedIntValue);

ByteBuffer allows you to change the byte order to little endian if you need that.

I found the class EndianUtils

But I'm not sure if it's what I'm looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM