4

I have an counter which counts from 0 to 32767

At each step, I want to convert the counter (int) to an 2 byte array.

I've tried this, but I got a BufferOverflowException exception:

byte[] bytearray = ByteBuffer.allocate(2).putInt(counter).array();
3
  • Well, what do you expect? You try to put 4 bytes into a buffer which is 2 bytes long. Commented Sep 17, 2015 at 9:12
  • What are you trying to achieve ? Commented Sep 17, 2015 at 9:13
  • changed max number to 32767 If I do that by shifting, the value 128 will be -128. But I need positiv bytes Commented Sep 17, 2015 at 9:15

3 Answers 3

4

Yes, this is because an int takes 4 bytes in a buffer, regardless of the value.

ByteBuffer.putInt is clear about both this and the exception:

Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.

...

Throws:
BufferOverflowException - If there are fewer than four bytes remaining in this buffer

To write two bytes, use putShort instead... and ideally change your counter variable to be a short as well, to make it clear what the range is expected to be.

Sign up to request clarification or add additional context in comments.

2 Comments

It works by using short, but if the counter reaches 128, the converted byte will be -128 instead of 128
@Struct: It will be the appropriate set of bits - it's just that byte in Java is (unfortunately) signed. There is no byte value of 128. Whatever converts the two-byte value back into a short will get the right value out.
1

First, you seem to assume that the int is big endian. Well, this is Java so it will certainly be the case.

Second, your error is expected: an int is 4 bytes.

Since you want the two last bytes, you can do that without having to go through a byte buffer:

public static byte[] toBytes(final int counter)
{
    final byte[] ret = new byte[2];
    ret[0] = (byte) ((counter & 0xff00) >> 8);
    ret[1] = (byte) (counter & 0xff);
    return ret;
}

You could also use a ByteBuffer, of course:

public static byte[] toBytes(final int counter)
{
    // Integer.BYTES is there since Java 8
    final ByteBuffer buf = ByteBuffer.allocate(Integer.BYTES);
    buf.put(counter);
    final byte[] ret = new byte[2];
    // Skip the first two bytes, then put into the array
    buf.position(2);
    buf.put(ret);
    return ret;
}

1 Comment

it seems there are 2 typos in ByteBuffer example: buf.put(counter) -> buf.putInt(counter); buf.put(ret) -> buf.get(ret);
0

This should work

Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two.

byte[] bytearray = ByteBuffer.allocate(2).putShort((short)counter).array();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.