Implementation of sms cat Software (C #) PDU Format Decoding C # implementation

Source: Internet
Author: User
Tags sca

The decoding part was simply completed yesterday, and the encoding part is added today (in the same class ). This method is similar to the encoding part. The attribute is used to decode fields.ProgramWritten in the attribute access section. The decoded function only needs to read these attribute values to complete PDU decoding.

The PDU string received by the mobile phone or sms cat contains eight parts: SCA, PDU-type, OA, PID, DCs, SCTS, udl, and Ud. Each part contains the corresponding fields and attributes, stores the string of the corresponding segment of the PDU format encoding in the field (the function first saves the corresponding string to the corresponding field), and writes the attribute reading processCodeThe read value is the decoded data (text message content, mobile phone number, etc ).

The information obtained from the PDU must be read (decoded) in four parts: SCA, OA, SCTS, and Ud.

For information about how to parse the received PDU format, refer:
Implementation of sms cat Software (C #) <3> Analysis of PDU text messages

Example PDU string: receives a text message from 15050850677 with the content "hello ".

0891683110402505f0240ba15150800576f7000801112081600423044f60597d

    • SCA (Short Message Service Center) Partial decoding:

Field: servicecenteraddress; attribute: servicecenteraddress.

When decoding, first part Of the sca pdu string (0891683110402505f0) Write the servicecenteraddress field. The get part of the property accessors completes decoding. Get code:

1:Get
 
2: {
 
3:IntLen =Convert. Toint32 (servicecenteraddress. substring (0, 2); // obtain the SCA Length
 
4:StringResult = servicecenteraddress. substring (4, len-4); // remove the starting part.
 
5:// For the paritychange function, see the encoding in the previous article.
 
6:Result = paritychange (result); // parity
 
7:Result = result. trimend ('F','F'); // Remove the end f
8:ReturnResult;
 
9:}

In this way, you can obtain the short message center address by reading the servicecenteraddress attribute. The program can read the sample message center:8613010452500

    • OA (sender address) Partial decoding:

Field: originatoraddress; attribute: originatoraddress.

When decoding (0ba15150800576f7) Write the originatoraddress field. Similarly, the mobile phone number of the sender is read from the property.

Corresponding get accesser code:

 
1:Get
 
2: {
 
3:IntLen =Convert. Toint32 (originatoraddress. substring (0, 2), 16 );// Convert the hexadecimal string into an integer
 
4:StringResult =String. Empty;
5:If(LEN % 2 = 1)// The number length is an odd number, and F is added when the length is 1 encoded.
 
6:{
 
7:Len ++;
 
8:}
 
9:Result = originatoraddress. substring (4, Len); // remove the header
 
10:Result = paritychange (result). trimend ('F','F'); // Parity, and remove the end f
 
11: 
 
12:ReturnResult;
 
13:}

In this way, you can obtain the short message center address by reading the originatoraddress attribute. The program can read the sample message center:15050850677

    • SCTS (Service Center timestamp) Partial decoding:

Field: servicecentertimestamp; attribute: servicecentertimestamp.

When decoding, first part Of the scts pdu string (01112081600423) Write the servicecentertimestamp field. Similarly, the read time from the attribute is the time when the message center receives the information.

Corresponding get accesser code:

 
1:Get
 
2: {
 
3:StringResult = paritychange (servicecentertimestamp); // parity
 
4:Result ="20"+ Result. substring (0, 12 );// The "20" that begins with the annual addition"
 
5: 
 
6:ReturnResult;
 
7:}

Read the servicecentertimestamp attribute to obtain the time string:20101102180640Representative:2010-11-0218:06:44

    • UD (user data, text message content) Partial decoding:

      Field: userdata; attribute: userdata.

      When decoding, first put the UD part of the PDU string (4f60597d) Write the userdata field. Similarly, the text message content is read from the property.

      Corresponding get accesser code:

 
1:Get
 
2:{
 
3:IntLen =Convert. Toint32 (userdatalenghth, 16) * 2;
 
4:StringResult =String. Empty;
 
5:// Four groups, each of which is translated into a USC2 character
 
6:For(IntI = 0; I <Len; I + = 4)
7:{
 
8:StringTemp = userdata. substring (I, 4 );
 
9: 
 
10:IntByte1 =Convert. Toint16 (temp, 16 );
 
11: 
 
12:Result + = ((Char) Byte1). tostring ();
 
13:}
 
14: 
 
15:ReturnResult;
 
16:}

Access userdata. For example, you can get the text message "hello ".

    • Decoder:

The decoder must assign the corresponding PDU encoding value to the corresponding field, and then read each attribute to complete decoding:

 
1:/// <Summary>
2:///Complete Decoding of SMS messages in PDU format received by mobile phone or SMS cat. Currently, only Chinese encoding is supported.
 
3:///Unused DCS
 
4:/// </Summary>
 
5:/// <Param name = "strpdu">Sms pdu string</Param>
 
6:/// <Param name = "msgcenter">Short Message Service Center output</Param>
7:/// <Param name = "phone">Sender's mobile phone number output</Param>
 
8:/// <Param name = "MSG">Text message content output</Param>
 
9:/// <Param name = "time">Time string output</Param>
 
10:Public voidPdudecoder (StringStrpdu,Out stringMsgcenter,Out stringPhone,Out stringMSG,Out stringTime)
11:{
 
12:IntLensca =Convert. Toint32 (strpdu. substring (0, 2), 16) * 2 + 2;// The length of the Short Message Center
 
13:Servicecenteraddress = strpdu. substring (0, lensca );
 
14: 
 
15:IntLenoa =Convert. Toint32 (strpdu. substring (lensca + 2, 2), 16 );// OA Length
 
16:If(Lenoa % 2 = 1)// Add 1 F to an odd number
17:{
 
18:Lenoa ++;
 
19:}
 
20:Lenoa + = 4;// The length of the header encoded with a number
 
21:Originatoraddress = strpdu. substring (lensca + 2, lenoa );
 
22: 
 
23:Servicecentertimestamp = strpdu. substring (lensca + lenoa + 6, 14 );
 
24: 
 
25:Userdatalenghth = strpdu. substring (lensca + lenoa + 20, 2 );
26:IntLenud =Convert. Toint32 (userdatalenghth, 16) * 2;
 
27:Userdata = strpdu. substring (lensca + lenoa + 22, lenud );
 
28: 
 
29:Msgcenter = servicecenteraddress;
 
30:Phone = originatoraddress;
 
31:MSG = userdata;
 
32:Time = servicecentertimestamp;
 
33:}

caller PE. pdudecoder ("plugin", out S1, out S2, out S3, out S4); (PE: pduencoding Pe = new pduencoding ();)
output:
S1: 8613010452500

S2:15050850677
S2:Hi!
S3:20101102180640
Successful!

The encoding is complete.

The implementation is relatively simple, and 7bit and 8bit encoding/decoding are not implemented. If you are interested, you can download the attachment and add it yourself.

Attachment: project documents

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.