【Java】java中解析TLV标记字符串

2023-12-22 10:56:57

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

            }

        }
    }
}

文章来源:https://blog.csdn.net/wjianwei666/article/details/135113427
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。