如何在java中解析TLV标记字符串

时间:2018-03-12 17:07:07

标签: java tlv

我正在尝试解析TLV字符串:

24 00 35 E1 31 9F 08 02 38 30 9F 37 04 4A 66 6B 69 DF AE 05 01 41 9F 26 08 73 30 35 76 4F 6A 36 4E DF AE 06 0C 33 34 71 76 69 70 6B 4D 53 63 66 55 9F 36 02 00 01 90 00 7B

在这种情况下,9F08是标签,后跟长度字段和数据。我能够使用当前输入字符串获取所需的字段,但可能存在标记(" 9F08")也可能位于另一个字段的数据部分中的情况。我很困惑如何继续这个。

public void bleToPos(String hex) {
    sb=new StringBuilder();
    tlv=new TLVData();
    int noOfCharacters=0,src=0,dest=0;
    String epiolgue="",epligueData="";

    String [] hexArray=hex.split(" ");
    //Extract 1st 3 bytes and last 3 bytes. last 3 bytes should contain 90 00 otherwise not valid string
    epiolgue=hexArray[hexArray.length-3];
    epiolgue= epiolgue + hexArray[hexArray.length-2];
    epligueData = hexArray[hexArray.length-1];
    if(!epiolgue.equals("9000")) {
        System.out.println(" Not a valid tlv no need to process");
        return;
    }
    tlv.setEpilogue(epligueData);
    int arrayLength=hexArray.length;
    if(hexArray!=null && hexArray.length>0) {
        for(int i=0;i<hexArray.length;i++) {
            try {
                if(hexArray[i].equals("9F")) {
                    if((i+1)<arrayLength &&  hexArray[i+1].equals("08")) {
                        noOfCharacters= (char)Integer.parseInt(hexArray[i+2], 16);
                        src=i+3;
                        dest=src+noOfCharacters;
                        copyArrayElementsIntoString(src, dest, hexArray);
                        tlv.setVersion(sb.toString());
                        sb.setLength(0);
                    }
                    else if((i+1)<arrayLength && hexArray[i+1].equals("37")) {
                        noOfCharacters= (char)Integer.parseInt(hexArray[i+2], 16);
                        src=i+3;
                        dest=src+noOfCharacters;
                        copyArrayElementsIntoString(src, dest, hexArray);
                        tlv.setDssid(sb.toString());
                        sb.setLength(0);
                    }
                    else if((i+1)<arrayLength && hexArray[i+1].equals("26")) {
                        noOfCharacters= (char)Integer.parseInt(hexArray[i+2], 16);
                        src=i+3;
                        dest=src+noOfCharacters;
                        copyArrayElementsIntoString(src, dest, hexArray);
                        tlv.setMac(sb.toString());
                        sb.setLength(0);
                    }
                    else if((i+1)<arrayLength && hexArray[i+1].equals("36")) {
                        noOfCharacters= (char)Integer.parseInt(hexArray[i+2], 16);
                        src=i+3;
                        dest=src+noOfCharacters;
                        copyArrayElementsIntoString(src, dest, hexArray);
                        tlv.setAtc(sb.toString());
                        sb.setLength(0);
                    }
                    i=dest-1;
                    src=0;
                    dest=0;
                    continue;

                }
                if(hexArray[i].equals("DF")) {
                    if((i+1)<arrayLength && hexArray[i+1].equals("AE")) {

                        if((i+2)<arrayLength && hexArray[i+2].equals("05")) {
                            noOfCharacters= (char)Integer.parseInt(hexArray[i+3], 16);
                            src=i+4;
                            dest=src+noOfCharacters;
                            copyArrayElementsIntoString(src, dest, hexArray);
                            tlv.setCvb(sb.toString());
                            sb.setLength(0);
                        }
                        else if((i+2)<arrayLength && hexArray[i+2].equals("06")) {
                            noOfCharacters= (char)Integer.parseInt(hexArray[i+3], 16);
                            src=i+4;
                            dest=src+noOfCharacters;
                            copyArrayElementsIntoString(src, dest, hexArray);
                            tlv.setToken(sb.toString());
                            sb.setLength(0);
                        }

                    }
                    i=dest-1;
                    src=0;
                    dest=0;
                    continue;
                }
                /*if(hexArray[i].equals("90")) {
                    if((i+1)<arrayLength && hexArray[i+1].equals("00")) {

                    }
                }*/

            }catch(Exception e) {

            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题是对的。在找到Length后解析数据的Tag时,您可以忽略TagData内出现的重复Length的字节数。只考虑在Length字节Data之外找到的标记。