Implementation of sms cat Software (C #) 7bitpdu Coding

Source: Internet
Author: User
Tags sendmsg

A simple class library was completed some time ago. Although it can complete sending and receiving SMS messages, the class library is not perfect.
For example, the USC2 encoding is used to send an English text message. Each text message can contain up to 70 characters. If 7-bit encoding is used, each message can send 160 characters. This article adds the PDU Encoding Based on the previous class.

    • Solution: Same as before, only partCode.
      The PDU class changes the set accessors of userdata to complete the 7bit encoding and adds a 7bit encoding function. In addition, some function names are modified to facilitateProgramReadability.
      Add a parameter to the sendmsg overload of the gsmmodem class to determine whether it is 7bit encoding or USC2 encoding.
      For specific solutions, see the previous sectionArticle:
      Implementation of sms cat Software (C #) <4> PDU format encoding C # implementation
      Implementation of sms cat Software (C #) <5> PDU Format Decoding C # implementation
      Implementation of sms cat Software (C #) <6> sending and receiving sms c # implementation (API)
    • PDU type:

Userdata attributes:
Userdata only changes the set accessors for 7bit encoding.
EncodingAlgorithm:
High/low switching, each 8-bit height can be taken as a character. Example:
Test:
T: 01010100 E: 01100101 S: 01110011 T: 01110100
Go to the highest digit 0 and change it to 7 digits
T: 1010100 E: 1100101 S: 1110011 T: 1110100
High/low bit switching: tset
1110100 1110011 1100101 1010100
From a high position, every 8 digits is regarded as a single-digit group: 1 1010100 11 110010 100 11100 0000 1110
D4f29c0e completes encoding!
Source codeAs follows:

 
1:Set
 
2: {
3:If(Datacodingscheme ="08"| Datacodingscheme ="18")// USC2 encoding: complete USC2
 
 4:{
 
5:Userdata =String. Empty;
 
6:EncodingEncodingutf =Encoding. Bigendianunicode;
 
7: 
 
8:Byte[] Bytes = encodingutf. getbytes (Value);
9: 
 
10:For(IntI = 0; I <bytes. length; I ++)
 
11:{
 
12:Userdata + =Bitconverter. Tostring (bytes, I, 1 );
 
13:}
 
14:Userdatalenghth = (userdata. Length/2). tostring ("X2");
 
15:}
 
16:Else// Add the 7bit encoding content to facilitate the 7bit Encoding
17:{
 
18:Userdata =String. Empty;
 
19:Userdatalenghth =Value. Length. tostring ("X2");// 7-bit encoded user data length: Source String Length
 
20: 
 
21:EncodingEncodingasscii =Encoding. ASCII;
 
22:Byte[] Bytes = encodingasscii. getbytes (Value);
23: 
 
24:StringTemp =String. Empty;// Store the binary string of the intermediate string
 
25:StringTMP;
 
26:For(IntI =Value. Length; I> 0; I --)// High/low swap binary string
 
27:{
 
28:TMP =Convert. Tostring (Bytes [I-1], 2 );
29:While(TMP. Length <7)// Not enough 7 digits, complete
 
30:{
 
31:TMP ="0"+ TMP;
 
32:}
 
33:Temp + = TMP;
 
34:}
 
35: 
 
36:For(IntI = temp. length; I> 0; I-= 8)// Each 8-bit bitwise takes one character, that is, the encoding is completed and the high bitwise is taken as the low bitwise.
37:{
 
38:If(I> 8)
 
39:{
 
40:Userdata + =Convert. Toint32 (temp. substring (I-8, 8), 2). tostring ("X2");
 
41:}
 
42:Else
 
43:{
 
44:Userdata + =Convert. Toint32 (temp. substring (0, I), 2). tostring ("X2");
45:}
 
46:}
 
47: 
 
48:}
 
49:}

After adding, you only need to read the userdata field to complete the corresponding part of the encoding.

Pdu7bitencoder function:
The code for adding a new 7-bit encoding method is as follows:

1:Public StringPdu7bitencoder (StringPhone,StringText)
 
2:{
 
3:If(Text. size> 160)
 
4:{
 
5:Throw newException("The number of text messages exceeds 160");
 
6:}
 
7:Datacodingscheme ="00";
 
8:Destinationaddress = phone;
 
9:Userdata = text;
10: 
 
11:ReturnServicecenteraddress + protocoldataunittype
 
12:+ Messagereference + destinationaddress + protocolidentifer
 
13:+ Datacodingscheme + validityperiod + userdatalenghth + userdata;
 
14:}

The method only assigns values to the corresponding attribute, and returns the field string to complete 7bit decoding.

Other changes:

1:Public StringPduusc2encoder (StringPhone,StringText)

The original method name was changed from pduencoder to pduusc2encoder, so that the program has better readability.

    • Gsmmodem class:

Enumeration: To facilitate the differentiation of SMS types (USC2 encoding or 7bit encoding), add enumeration types.

 
1:Public EnumMsgtype{Ausc2, a7bit };// Enumeration text message type ausc2 a7bit: 7-bit encoding (both Chinese and English can be used, but 7-bit can send 160 characters, USC2 only 70)

Add the enumeration type msgtype to the namespace gsmmodem

Sendmsg overload:

1:Public voidSendmsg (StringPhone,StringMSG,MsgtypeMsgtype)
 
2:{
 
3:If(Msgtype =Msgtype. Ausc2)
 
4:{
 
5:Sendmsg (phone, MSG );
 
6:}
 
7:Else
 
8:{
 
9: 
 
10:}
11:}

If the type is ausc2, call the original sending method and use USC2 encoding to send text messages. If the a7bit type uses the 7bit encoding function of the pduencoding class, the program has not been implemented yet. The detailed source code will be provided in the next blog
Of course, the original calling of the PDU class encoding method is changed accordingly.

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.