How can I concatenate two bytes in java?

I have an integer writePos

that takes a value between [0,1023]

. I need to store it in the last two bytes of a named byte array bucket

. So, I believe I need to represent it as a concatenation of an array of the last two bytes.

  • How can I split writePos

    into two bytes that when concatenated and converted to int

    are called again writePos

    ?

  • How can I concatenate the concatenation once I get it in bytes?

+3


source to share


3 answers


This will cover the high level of ByteBuffer.

short loc = (short) writeLocation;

byte[] bucket = ...
int idex = bucket.length - 2;
ByteBuffer buf = ByteBuffer.wrap(bucket);
buf.order(ByteOrder.LITTLE__ENDIAN); // Optional
buf.putShort(index, loc);

writeLocation = buf.getShort(index);

      

The order can be specified or left by default (BIG_ENDIAN).

  • The ByteBuffer wraps the original byte array and also affects the effect of the ByteBuffer on the byte array.
  • It is possible to use sequential write and read positioning (search), but here I am using overloaded methods for immediate positioning with index

    .
  • putShort

    writes to a byte array, modifying two bytes, short.
  • getShort

    reads a short from an array of bytes, which can be put into an int.



Explanation

A short

in java is a two byte (signed) integer. And that's what it means. Order: LITTLE_ENDIAN: least significant byte (n% 256, n / 256) or high endian.

+2


source


Bitwise operations.

Byte:

byte[] bytes = new byte[2];
// This uses a bitwise and (&) to take only the last 8 bits of i
byte[0] = (byte)(i & 0xff); 
// This uses a bitwise and (&) to take the 9th to 16th bits of i
// It then uses a right shift (>>)  then move them right 8 bits
byte[1] = (byte)((i & 0xff00) >> 8);from byte:

      



To go back to the other side

// This just reverses the shift, no need for masking.
// The & here is used to handle complications coming from the sign bit that
// will otherwise be moved as the bytes are combined together and converted
// into an int
i = (byte[0] & 0xFF)+(byte[1] & 0xFF)<<8;

      

There is a working example of some conversions you can play with here: http://ideone.com/eRzsun

+1


source


You need to split the integer into two bytes. High and low byte. Following your description, it is stored as bug-endian in an array.

int writeLocation = 511;
byte[] bucket = new byte[10];
// range checks must be done before
// bitwise right rotation by 8 bits
bucket[8] = (byte) (writeLocation >> 8); // the high byte
bucket[9] = (byte) (writeLocation & 0xFF); // the low byte

System.out.println("bytes = " + Arrays.toString(bucket));

// convert back the integer value 511 from the two bytes
bucket[8] = 1;
bucket[9] = (byte) (0xFF);
// the high byte will bit bitwise left rotated
// the low byte will be converted into an int
// and only the last 8 bits will be added
writeLocation = (bucket[8] << 8) + (((int) bucket[9]) &  0xFF);
System.out.println("writeLocation = " + writeLocation);

      

0


source







All Articles