Java PDU Short Message decoding comprehensive analysis _java

Source: Internet
Author: User

Long and long letter not verified interested can try

Rewritten according to the Python method.

/** * PDU SMS Resolution * * * @param pdupayload * @return/public static String Retrievesmsinfo (byte[] pdupayload) throws Unsuppo  rtedencodingexception {int startpos = 3;//#Originator address int mrp_oa_len = Pdupayload[startpos]; byte[] Mrp_oa = new
Byte[mrp_oa_len];
System.arraycopy (Pdupayload, startpos + 1, Mrp_oa, 0, Mrp_oa_len);
startpos = startpos + 1 + mrp_oa_len;
int mtpdu_len = Pdupayload[startpos]; #BIT No. 7 6 5 4 3 2 1 0//#uplink tp-rp tp-udhi tp-spr tp-vpf tp-rd tp-mti//#downlink tp-rp tp-udhi Tp-sri Tp-mms TP
-MTI byte Tp_header = pdupayload[startpos + 1];
byte Tp_msg_ref = Pdupayload[startpos + 2]; int Tp_udhi = (tp_header >> 6) & 1; #短信内容是否包含协议头信息, 0 does not contain, 1 contains (short letter, push sms) int TP_VPF = (tp_header >> 3) & 3; 
#是否包含有效期字节, 0 not included, other include//#00表示无有效期, TP-VP set to 00. 
#10表示相对格式, TP-VP occupies 1 bytes. 
#01表示增加格式, TP-VP occupies 7 bytes. #11表示绝对格式, TP-VP occupies 7 byte int tp_mms = (tp_header >> 2) & 1;//# Tp-mms (tp-more-message-to-send): 1 Message Center No more messages sent St Artpos = Startpos +3;
#对方号码 byte Smsnumberlen = Pdupayload[startpos];
int Mtp_da_len = (Smsnumberlen + 1)/2 + 1;
byte[] Mtp_da = new Byte[mtp_da_len];
System.arraycopy (Pdupayload, startpos + 1, mtp_da, 0, Mtp_da_len * 1);
byte Mtp_da_format = mtp_da[0];
byte[] Smsnumberraw = new Byte[mtp_da.length-1];
System.arraycopy (Mtp_da, 1, smsnumberraw, 0, mtp_da.length-1);
String smsnumber = "";
int j = 0; for (int i = 0; i < Smsnumberlen i++) {if ((I & 1) = = 0) {Smsnumber = smsnumber + (int) (Smsnumberraw[j] & 0
XF);
else {Smsnumber = smsnumber + (int) (Smsnumberraw[j] & 0x0ff) >> 4; j + +}}
startpos = startpos + 1 + mtp_da_len;
byte mtp_pid = Pdupayload[startpos];
byte Mtp_dcs = pdupayload[startpos + 1];//# "00" means 7-bit encoding, set to "02" Using 8-bit encoding, set to "08" Using UCS2 encoding.
startpos = startpos + 2; if (TP_VPF = = 2) {startpos = startpos + 1;} else if (TP_VPF = = 1 | | TP_VPF = = 3) {startpos = startpos + 7;}//# long letter: Content before the need to increase 6 fields//# 1, byte one: Baotou length, fixed fill 0x05;//# 2, byte two: Baotou type identification, fixed fill in 0x00, indicating length Letter;//# 3, ByteThree: The length of the child package, fixed fill 0x03, represents the length of the following three bytes;//# 4, byte four to byte six: package content://# a) byte four: Long message reference number, each SP to send each user a reference number should be different, can start from 0, each plus 1, maximum 255, It is convenient for the same terminal to identify different short and long letters of messages from the same SP;//# b) byte Five: The total number of messages in this long message, from 1 to 255, should generally be greater than 2;//# c) byte Six: The position or serial number of this message in a long message, from 1 to 255, the first is 1, and the second is 2, 
The last value equals four bytes.
# example://# (//#) int smspayloadlen = Pdupayload[startpos]);
startpos = startpos + 1;
String smscontent = ""; 
if (Tp_udhi = = 1) {//#长短信-No validation may be required to turn unsigned byte smstotal = Pdupayload[startpos + 4]; byte Smsidx = Pdupayload[startpos + 5];
startpos = startpos + 6;
Smscontent = "Long Letter (" + Bytetohex (SMSIDX) + "/" + Bytetohex (smstotal) + "}";
Smscontent = new String (smscontent.getbytes ("GBK"));
Smspayloadlen = smsPayloadLen-6;
} byte[] Smspayload = new Byte[pdupayload.length-startpos];
System.arraycopy (Pdupayload, startpos, smspayload, 0, Pdupayload.length-startpos); if (Mtp_dcs = = 0) {//#7位编码-verified Smspayloadlen = (Smspayloadlen * 7 + 7)/8; int asciidata = 0; int lastbyteremain = 0; F or (int i = 0; i < SmspayloaDlen; i++) {Asciidata = Asciidata + ((smspayload[i) & 0x0ff) << lastbyteremain); smscontent = smscontent + (char) (a
Sciidata & 0x0ff) & 0x7f);
Asciidata = Asciidata >> 7;
Lastbyteremain = Lastbyteremain + 1;  if (Lastbyteremain >= 7) {smscontent = smscontent + (char) (Asciidata & 0x0ff) & 0x7f); asciidata = Asciidata
>> 7;
Lastbyteremain = lastByteRemain-7; }} else if (Mtp_dcs = 8) {//# UCS-2-verified to parse for (int i = 0; i < smspayloadlen i = i + 2) {int cc1 = (smspayl
Oad[i] & 0X0FF) * 256;
int CC2 = smspayload[i + 1] & 0x0ff;
Smscontent = smscontent + (char) (CC1 + CC2); } return Smsnumber + ":" + smscontent;}

The above is a small series to introduce the Java PDU SMS decoding comprehensive analysis, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.