Parsing byte array containg fields of unknown length

后端 未结 5 1328
鱼传尺愫
鱼传尺愫 2021-01-12 15:26

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         


        
5条回答
  •  再見小時候
    2021-01-12 16:06

    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);
    

提交回复
热议问题