Java pdu SMS decoding and javapdu SMS

Source: Internet
Author: User

Java pdu SMS decoding and javapdu SMS

Java pdu SMS Decoding

If you are interested in long text message verification, try it.

Rewritten according to the python Method

1/** 2 * pdu sms resolution 3*4*5 * @ param pduPayload 6 * @ return 7 */8 public static String retrieveSMSInfo (byte [] pduPayload) throws UnsupportedEncodingException {9 10 int startPos = 3; 11 // # Originator address 12 int mRP_OA_len = pduPayload [startPos]; 13 byte [] mRP_OA = new byte [mRP_OA_len]; 14 System. arraycopy (pduPayload, startPos + 1, mRP_OA, 0, mRP_OA_len); 15 startPos = startPos + 1 + mRP_OA_len; 16 int mTPDU_len = pduPayload [startPos]; 17 // # BIT No. 7 6 5 4 3 2 1 0 18 // # uplink TP-RP TP-UDHI TP-SPR TP-VPF TP-RD TP-MTI 19 // # downlink TP-RP TP-UDHI TP-SRI TP-MMS TP-MTI 20 byte TP_Header = pduPayload [startPos + 1]; 21 byte TP_Msg_Ref = pduPayload [startPos + 2]; 22 int TP_UDHI = (TP_Header> 6) & 1; // # whether the text message content contains the protocol header information, 0 does not include, 1 contains (long SMS, push SMS) 23 int TP_VPF = (TP_Header> 3) & 3; // # whether it contains valid bytes, 0 does not contain, other contains 24 // #00 indicates no validity period, TP-VP is set to 00. 25 // #10 indicates the relative format, the TP-VP occupies 1 byte. 26 // #01 indicates adding the format, the TP-VP occupies 7 bytes. 27 // #11 indicates absolute format, TP-VP occupies 7 bytes 28 int TP_MMS = (TP_Header> 2) & 1; // # TP-MMS (TP-More-Message-to-Send): 1 sms center no More messages sent 29 startPos = startPos + 3; 30 // # peer number 31 byte smsNumberLen = pduPayload [startPos]; 32 int mTP_DA_len = (smsNumberLen + 1)/2 + 1; 33 byte [] mTP_DA = new byte [mTP_DA_len]; 34 System. arraycopy (pduPayload, startPos + 1, mTP_DA, 0, mTP_DA_len * 1); 35 byte mTP_DA_format = mTP_DA [0]; 36 Byte [] smsNumberRaw = new byte [mTP_DA.length-1]; 37 System. arraycopy (mTP_DA, 1, smsNumberRaw, 0, mTP_DA.length-1); 38 String smsNumber = ""; 39 int j = 0; 40 for (int I = 0; I <smsNumberLen; I ++) {41 42 if (I & 1) = 0) {43 smsNumber = smsNumber + (int) (smsNumberRaw [j] & 0xF ); 44} else {45 smsNumber = smsNumber + (int) (smsNumberRaw [j] & 0x0FF)> 4); 46 j ++; 47} 48} 49 startPos = s TartPos + 1 + mTP_DA_len; 50 51 byte mTP_PID = pduPayload [startPos]; 52 byte mTP_DCS = pduPayload [startPos + 1]; // # "00" indicates 7-bit encoding, set "02" to use 8-bit encoding and "08" to use UCS2 encoding. 53 54 startPos = startPos + 2; 55 if (TP_VPF = 2) {56 startPos = startPos + 1; 57} else if (TP_VPF = 1 | TP_VPF = 3) {58 startPos = startPos + 7; 59} 60 // # long text message: six fields must be added before the content. 61 // #1. byte 1: header length, fixed fill 0x05; 62 // #2; byte 2: Header type identifier; Fixed fill 0x00, which indicates long text message; 63 // #3. byte 3: the length of the sub-package. It must be set to 0x03, indicating the length of the next three bytes. 64 // #4. Four to six Bytes: Package content: 65 // #) byte 4: Long Message Reference Number. Each SP should send a different reference number to each user. It can start from 0 and Add 1 at a time, up to 255, this makes it easy for a terminal to recognize different long messages of the same SP. Farewell; 66 // # B) byte 5: Total number of long messages in this Section, from 1 to 255. The general value should be greater than 2; 67 // # c) byte 6: the position or serial number of a message in a long message, from 1 to 255. The first message is 1, the second message is 2, and the last message is equal to the value of the fourth byte. 68 // # example: 69 // #05 00 03 00 02 01 70 // #05 00 03 00 02 02 71 int smsPayloadLen = pduPayload [startPos]; 72 startPos = startPos + 1; 73 String smsContent = ""; 74 75 if (TP_UDHI = 1) {76 // # long text message-unverified may need to be converted to the unsigned 77 byte smsTotal = pduPayload [startPos + 4]; 78 byte smsIdx = pduPayload [startPos + 5]; 79 startPos = startPos + 6; 80 smsContent = "long message (" + byteToHex (smsIdx) + "/" + byteToHex (smsTotal) + "}"; 81 smsContent = new String (smsContent. getBytes ("gbk"); 82 smsPayloadLen = smsPayloadLen-6; 83} 84 byte [] smsPayload = new byte [pduPayload. length-startPos]; 85 System. arraycopy (pduPayload, startPos, smsPayload, 0, pduPayload. length-startPos); 86 87 if (mTP_DCS = 0) {88 // #7-bit encoding-89 smsPayloadLen verified = (smsPayloadLen * 7 + 7)/8; 90 int asciiData = 0; 91 int lastByteRemain = 0; 92 for (int I = 0; I <smsPayloadLen; I ++) {93 asciiData = asciiData + (smsPayload [I] & 0x0FF) <lastByteRemain); 94 smsContent = smsContent + (char) (asciiData & 0x0FF) & 0x7f ); 95 asciiData = asciiData> 7; 96 lastByteRemain = lastByteRemain + 1; 97 98 if (lastByteRemain> = 7) {99 smsContent = smsContent + (char) (asciiData & 0x0FF) & 0x7f); 100 asciiData = asciiData> 7; 101 lastByteRemain = lastByteRemain-7; 102} 103} 104} else if (mTP_DCS = 8) {105 // # UCS-2 -- successfully resolved 106 for (int I = 0; I <smsPayloadLen; I = I + 2) {107 int C0 = (smsPayload [I] & 0x0FF) * 256; 108 int cc2 = smsPayload [I + 1] & 0x0FF; 109 smsContent = smsContent + (char) (PC3 + cc2); 110 111} 112} 113 return smsNumber + ":" + smsContent; 114}
View Code

 

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.