I have following data fields:
int iData1 = 100;
int iData2 = 5000;
float fData3 = 80.5f;
float fData4 = 100.1f;
String str1 = "BBBB";
Sending data as byte array as below:
ByteBuffer buf=ByteBuffer.allocate(BUF_SIZE);
buf.order(ByteOrder.BIG_ENDIAN);
buf.putInt(bData1);
buf.putInt(iData2);
buf.putFloat(fData3);
buf.putFloat(fData4);
buf.put(str1.getBytes());
sendBytes(buf.array());
To parse the byte array received, I can get each field a follow:
iData1 = bbf.getInt();
iData2 = bbf.getInt();
iData3 = bbf.getInt();
fData3 = bbf.getFloat();
fData4 = bbf.getFloat();
But how to get the string data field (str1) in the array received?
Any hint would be highly appreciated.
Thanks!
Put the length of the string before the string. Then read the string in two steps: