I have a byte array I want to assign as follows:
- First byte specifies the length of the string:
(byte)string.length() - 2nd – Last bytes contain string data from
string.getBytes()
Other than using a for loop, is there a quick way to initialize a byte array using bytes from two different variables?
You can use
System.arrayCopy()to copy your bytes:Though using something like a
ByteArrayOutputStreamor aByteBufferlike other people suggested is probably a cleaner approach and will be better for your in the long run 🙂