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

Source: Internet
Author: User

There is a serious problem in the class library implemented a few days ago. The received ASCII code and the 7-bit SMS read text message content is garbled, so that the control cannot be used. The reason is that there is no corresponding decoding in the class library.Program, All are decoded by USC2 encoding. Add the program described in this article to this PartCodeTo solve this problem, the previous project file updates this part at the same time. You are welcome to download this part.

Let's take a look at the 7bit encoding of the PDU:
For more information about PDU encoding, see the implementation of the sms cat Software (C #) <3> Analysis of PDU text messages

English code: 7-bit code, which shifts the last few digits of the next digit to the front to form a new 8-bit code.
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
Move the back low to the front to Form 8-bit encoding
Test: 11010100111100101001110000001110
UD: d4f29c0e udl: 04

DecodingAlgorithm: Use a loop; read a single-digit group (saved in the byte buffer) cyclically at a time; convert it to a binary string, and combine the low position with the remaining binary digits of the corresponding previous one-digit group to form a 7bit ASCII code, add the result array.

 
1:Private stringPdu7bitdecoder (StringUserdata)
 
2:{
 
3:StringResult =String. Empty;
 
4:Byte[] B =New byte[100];
 
5:StringTemp =String. Empty;
 
6: 
7:For(IntI = 0; I <userdata. length; I + = 2)
 
8:{
 
9:B [I/2] = (Byte)Convert. Tobyte (userdata [I]. tostring () + userdata [I + 1]. tostring (), 16 );
 
10:}
 
11: 
 
12:IntJ = 0;// While count
 
13:IntTMP = 1;// Number of binary characters in temp
14:While(J <userdata. Length/2-1)
 
15:{
 
16:StringS =String. Empty;
 
17: 
 
18:S =Convert. Tostring (B [J], 2 );
 
19: 
 
20:While(S. Length <8)// If the number of bytes converted by the complement of 8 bytes is less than 8 bits, decoding will lead to an error.
 
21:{
22:S ="0"+ S;
 
23:}
 
24: 
 
25:Result + = (Char)Convert. Toint32 (S. substring (TMP) + temp, 2 );// Add a character to the result set temp and the remaining one in the previous group
 
26:
 
27:Temp = S. substring (0, TMP );// Multiple parts in the previous group
 
28:
29:If(TMP> 6)// Add a character when the excess part is 7 characters long.
 
30:{
 
31:Result + = (Char)Convert. Toint32 (temp, 2 );
 
32:Temp =String. Empty;
 
33:TMP = 0;
 
34:}
 
35: 
 
36:TMP ++;
 
37:J ++;
38: 
 
39:If(J = userdata. Length/2-1)// The last character
 
40:{
 
41:Result + = (Char)Convert. Toint32 (Convert. Tostring (B [J], 2) + temp, 2 );
 
42:}
 
43:}
 
44:ReturnResult;
 
45:}

Other changes in the Class Library:

  • Userdata get accessors: Add calls to this part of the Code:

     
    1:Get
     
    2:{
    3:IntLen =Convert. Toint32 (userdatalenghth, 16) * 2;
     
    4:StringResult =String. Empty;
     
    5: 
     
    6:If(Datacodingscheme ="08"| Datacodingscheme ="18") Add new judgment// USC2 Encoding
     
    7:{
     
    8:// Four groups, each of which is translated into a USC2 character
    9:For(IntI = 0; I <Len; I + = 4)
     
    10:{
     
    11:StringTemp = userdata. substring (I, 4 );
     
    12: 
     
    13:IntByte1 =Convert. Toint16 (temp, 16 );
     
    14: 
     
    15:Result + = ((Char) Byte1). tostring ();
     
    16:}
    17:}
     
    18:Else
     
    19:{
     
    20:Result = pdu7bitdecoder (userdata); Add the call 7bit
     
    21:}
     
    22: 
     
    23:ReturnResult;
     
    24:}

    The original code does not use DCS (during decoding). Now you need to add:

  • Decoding function: Add DCs to write data
 
1:Datacodingscheme = strpdu. substring (lensca + lenoa + 4, 2 );// Assign a value to DCs to distinguish decoding 7bit

Also changed

1:Userdata = strpdu. substring (lensca + lenoa + 22 );

Sentence. The original length of the sub-string, because the length of the 7-bit encoding is not the same as that of the USC2 encoding, remove this parameter and get it directly to the end of the string.

    • Now, an available class library is complete, although not very powerful.

      Where the code is changed: the addition of 7-bit encoding. Currently, the use of USC2 encoding is used for sending ASCII text messages, which is a waste of space. (A 7-bit text message can contain 160 characters, USC2 is only 70 characters ).

If you want the cat program implemented in the previous article to recognize the 7-bit encoding (only receive), you can overwrite the DLL file generated today to the file used in the previous article. You do not need to change it elsewhere.

Thank you for your support. We welcome your comments.

Updated: The while loop of the decoding function is changed to the do-while loop to solve the problem that the decoded content is empty when only one English character is received. The modified content of the file is in:

Implementation of the sms cat Software (C #) series blog Index

Attachment: project documents

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.