Parsing byte array containg fields of unknown length

后端 未结 5 1346
鱼传尺愫
鱼传尺愫 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 15:57

    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
    

提交回复
热议问题