How to efficiently convert byte array to string
Use the String(bytes[] bytes, int offset, int length)
constructor: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#String(byte[], int, int)
new String(b, 0, 5);
new String(b, 0 ,5);
See the API doc for more information.
If you need to create a string for each region in the record, I would suggest a substring approach:
byte[] wholeRecord = {0,1,2 .. all record goes here .. 151}
String wholeString = new String(wholeRecord);
String id = wholeString.substring(0,1);
String refId = wholeString.substring(1,3);
...
The actual offsets may be different depending on string encoding.
The advantage of this approach is that the byte array is only copied once. Subsequent calls to substring()
will not create copies, but will simply reference the first copy with offsets. So you can save some memory and array copying time.