如何将消息解码为给定的形式

  • 本文关键字:消息 解码 java activemq
  • 更新时间 :
  • 英文 :


我有一条由消费者接收的activemq消息,如下所示:

0327700000260000460000010000047000108Full TalkValue Offer! Get talkvalue of Rs.62 on Recharge of Rs.62.Yourlast Call Charge is 1.000.Your Main Balance is Rs 47.000.00001500001291965355668000001800001604952312808659f9

我必须使用java:将此消息解码为以下格式

DIALOG : 032770
MESSAGE : 000026
Parameter : 000046 [ UNKNOWN ] Length : 1
Value : 0
Parameter : 000047 [ UNKNOWN ] Length : 108
Value : FullTalkValueOffer!GettalkvalueofRs.62onRechargeofRs.62.YourlastCallChargeis1.000.YourMainBalanceisRs47.000.
Parameter : 000015 [ MSISDN ] Length : 12
Value : 919653556680
Parameter : 000018 [ UNKNOWN ] Length : 16
Value : 04952312808659f9

使用以下规则对消息进行解码:消息的前6个字符是DIALOG,后6个字符为message。之后,它将选择接下来的6个字符作为参数。并将在接下来的6个字符中搜索长度。它将忽略0,并选择1。如果在这6个字符中的任何位置,它将选择该字符,长度将是1和1之后的数字。根据该长度,它将选取消息的下一个字符作为值。之后,它将选择下一个参数以及相应的长度和值。

我已经使用字符串的子字符串方法解码了对话框和消息。但我找不到解码进一步消息的逻辑。。请告诉我其中的逻辑。。

public class Message {
    public int dialog;
    public int message;
    public Map<Integer, String> parameters;
    public Message(String input) {
        int pos = 0;
        dialog = Integer.parseInt(input.substring(pos,pos+6));
        pos += 6;
        message = Integer.parseInt(input.substring(pos,pos+6));
        pos += 6;
        parameters = new TreeMap<Integer,String>();
        while (pos+12 <= input.length())
        {
            int param = Integer.parseInt(input.substring(pos,pos+6));
            pos += 6;
            int len = Integer.parseInt(input.substring(pos,pos+6));
            pos += 6;
            parameters.put(param, input.substring(pos,pos+len));
            pos += len;
        }
    }
}
Message msg = new Message(input);
System.out.printf("DIALOG : %dn", msg.dialog);
System.out.printf("MESSAGE : %dn", msg.message);
for (Integer param : msg.parameters.keySet()) {
    System.out.printf("PARAM %d : "%s"n", param, msg.parameters.get(param));
}

最新更新