Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I take the first "n" elements of a byte array and convert them directly into a string?

I have an array of bytes consisting of 1024 elements. I want to break this down into different string private members (e.g. first 9 bytes for name, next 12 bytes for userID, etc.).

Without having to turn the entire byte array into a string and then using a substring method, is there any way I can turn a range of bytes in the array directly into a private member for my class?

E.g.

myObject.name = byteArr[0-9];
myObject.userId = byteArr[10-21];
like image 660
AlwaysQuestioning Avatar asked Apr 21 '15 20:04

AlwaysQuestioning


1 Answers

Use:

String myField = new String(myArray, start, end);

where start would be 0 if you want to start from the beginning

like image 149
morgano Avatar answered Sep 25 '22 12:09

morgano