Jebathon
Jebathon

Reputation: 4561

Java - fill in specific indices for a byte array

Suppose I have a byte array with 65 indices, and I want to fill the first 62 indices (or bytes) with data since index 63-65 are reserved. How can I move a byte array to the first 62 indices of the byte array?

String message = "Hello to the client. This is the message that you will receive"; //62 bytes message
byte[] b = message.getBytes();

byte[] sendData = new byte[65];
//how can I transfer byte[] b to 0-62?

Upvotes: 0

Views: 366

Answers (2)

user2390182
user2390182

Reputation: 73460

Loop through the 62 indeces (0-61 btw) and copy the cells.

for (int i = 0; i < b.length; i++) {
    sendData[i] = b[i];
}

Upvotes: 1

Dylan Wheeler
Dylan Wheeler

Reputation: 7074

I'm not entirely sure what you're getting at, but I think the Arrays.copy method will help you. With this method, you can copy certain parts of an array to other arrays, saving the sections you're concerned about.

Upvotes: 0

Related Questions