I am parsing in Java a byte array having the following specification:
Trace data format:
- 4 bytes containing the Id.
- 4 bytes containing the address.
- N bytes containing the first name, where 0 < N < 32
- N bytes containing the last name, where 0 < N < 32
- 4 bytes containing the Minimum
- 4 bytes containing the Maximum
- 4 bytes containing the Resource Default Level
Today I don't see any solution to parse this array in order to get 7 variable with the correct type. Do you confirm or Am I missing something like a magic function in Java allowing to find String "limits" in a byte array (I can't see how the Minimum value can be distincted from its associated ASCII character).
Is there any "convention" about a special character between the 2 strings ?
Well, you know that the first name starts at byte 9, and that the last name ends at byte (lenght-13). What is uncertain is how to find where the first name ends and the last name begins. I see a few possible soutions:
writeUTF()
, which means that the specification of the byte count is most likely wrong. However, this at least specifies the encoding, which is otherwise an open question.Is there any "convention" about a special character between the 2 strings ?
Well c-strings are often null-terminated \0
.
If there is no such character I would say that it is impossible to parse the structure.
Assuming the first and last name are null-terminated you would do it like this:
int firstNameLength = 0;
while(firstNameLength<32) {
if(theArray[firstNameLength]=='0') break;
firstNameLength++;
}
int lastNameLength = 0;
while(lastNameLength<32) {
if(theArray[8+firstNameLength+1+lastNameLength]=='0') break;
i++;
}
String firstName = new String(theArray).substring(8,8+firstNameLength);
String lastName = new String(theArray).substring(8+firstNameLength+1,8+firstNameLength+1+lastNameLength);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With