0

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?
3
  • There is no such thing as a 62 bytes message, because getBytes depends on an encoding. Can you be sure that 62 bytes will always be enough? Commented Nov 26, 2015 at 21:36
  • I checked if it was 62 bytes by running b.length. Commented Nov 26, 2015 at 21:38
  • I know, but that result is platform-dependent. See the docs for String.getBytes. Commented Nov 26, 2015 at 21:39

2 Answers 2

1

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

for (int i = 0; i < b.length; i++) {
    sendData[i] = b[i];
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.