I am parsing in Java a byte array having the following specification:
Trace data format:
- 4 bytes containing the Id.
- 4 bytes containing the addres
if you want to read N ASCII bytes and turn them into a String.
public static String readString(DataInputStream dis, int num) throws IOException {
byte[] bytes = new byte[num];
dis.readFully(bytes);
return new String(bytes, 0);
}
For the rest of the values, you can use
dis.readInt();
If you are asking if there is any way to know how long the strings are, I don't believe you can determine this from the information provided. Perhaps the strings are '0' byte terminated or have the length as the first byte. Perhaps if you look at the bytes in the file you will see what the format is.
od -xc my-format.bin
Just to add another possibility for Michael's answer.
Assuming that N
is the same for both fields, and since the same letter is used I would guess that this is the case, the field positions would be like this:
int len = array.length;
int varLen = len - 5*4;
int fieldPos[] = new int[7];
fieldPos[0] = 0;
fieldPos[1] = 4;
fieldPos[2] = 8;
fieldPos[3] = 8 + varLen;
fieldPos[4] = 8 + 2*varLen;
fieldPos[5] = 8 + 2*varLen + 4;
fieldPos[6] = 8 + 2*varLen + 8;
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);
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.
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: