Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a Byte array

Is it possible to get specific bytes from a byte array in java?

I have a byte array:

byte[] abc = new byte[512];  

and i want to have 3 different byte arrays from this array.

  1. byte 0-127
  2. byte 128-255
  3. byte256-511.

I tried abc.read(byte[], offset,length) but it works only if I give offset as 0, for any other value it throws an IndexOutOfbounds exception.

What am I doing wrong?

like image 627
Tara Singh Avatar asked Feb 12 '10 17:02

Tara Singh


People also ask

How to split a byte array?

Split Byte Array In Java, we can use ByteBuffer or System. arraycopy to split a single byte array into multiple byte arrays. For example, this 000102030a0b0c0d1a1b1c1d2f2f is a byte array (14 bytes) in hex representation, it is a combined of cipher (8 bytes) + nonce (4 bytes) + extra (2 bytes).

How to combine byte arrays Java?

To concatenate multiple byte arrays, you can use the Bytes. concat() method, which can take any number of arrays.

How do you split bytes in Python?

Solution: To split a byte string into a list of lines—each line being a byte string itself—use the Bytes. split(delimiter) method and use the Bytes newline character b'\n' as a delimiter.

What is ByteBuffer in Java?

ByteBuffer holds a sequence of integer values to be used in an I/O operation. The ByteBuffer class provides the following four categories of operations upon long buffers: Absolute and relative get method that read single bytes. Absolute and relative put methods that write single bytes.


1 Answers

You can use Arrays.copyOfRange() for that.

like image 178
tangens Avatar answered Sep 20 '22 19:09

tangens