SMS Encoder & Concatenated EMS Encoder (短信编码器)

 

None.gif ' #########################################
None.gif'
SMS/EMS Encoder
None.gif'
Write by Hesicong
None.gif'
Last Edited:2005/2/20
None.gif'
Until now no bug found here.
None.gif'
Contact:
None.gif'
   Email:
None.gif'
       hesicong@mail.sc.cninfo.net
None.gif'
   or
None.gif'
       38288890@qq.com
None.gif'
   HomePage:
None.gif'
       http://dream-world.nease.net
None.gif'
Thanks for using my code
None.gif'
#########################################
ExpandedBlockStart.gifContractedBlock.gif
Namespace SMS Namespace SMS
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Namespace EncoderNamespace Encoder
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Class SMSClass SMS
ExpandedSubBlockStart.gifContractedSubBlock.gif
Enums#Region "Enums"
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Enum ENUM_TP_VPFEnum ENUM_TP_VPF
InBlock.gif                Relative_Format 
= 16    'b4=1 b3=0
ExpandedSubBlockEnd.gif
            End Enum

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Enum ENUM_TP_SRIEnum ENUM_TP_SRI
InBlock.gif                Request_SMS_Report 
= 32
InBlock.gif                No_SMS_Report 
= 0
ExpandedSubBlockEnd.gif            
End Enum

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Enum ENUM_TP_DCSEnum ENUM_TP_DCS
InBlock.gif                DefaultAlphabet 
= 0
InBlock.gif                UCS2 
= 8
ExpandedSubBlockEnd.gif            
End Enum

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Enum ENUM_TP_VALID_PERIODEnum ENUM_TP_VALID_PERIOD
InBlock.gif                OneHour 
= 11 '0 to 143:(TP-VP+1)*5Min
InBlock.gif
                ThreeHours = 29
InBlock.gif                SixHours 
= 71
InBlock.gif                TwelveHours 
= 143
InBlock.gif                OneDay 
= 167
InBlock.gif                OneWeek 
= 196
InBlock.gif                Maximum 
= 255
ExpandedSubBlockEnd.gif            
End Enum

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Private Data#Region "Private Data"
InBlock.gif            
Protected SC_Number As String  'Note the plus!
InBlock.gif
            Protected TP_MTI As Byte = 1
InBlock.gif            
Protected TP_RD As Byte = 0
InBlock.gif            
Protected TP_VPF As Byte = 16
InBlock.gif            
Protected TP_UDHI As Byte
InBlock.gif            
Protected TP_SRR As Byte
InBlock.gif            
Protected TP_MR As Integer
InBlock.gif            
Protected TP_DA As String
InBlock.gif            
Protected TP_PID As Byte
InBlock.gif            
Protected TP_DCS As Byte
InBlock.gif            
Protected TP_VP As Byte
InBlock.gif            
Protected TP_UDL As Integer
InBlock.gif            
Protected TP_UD As String
ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Properties#Region "Properties"
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Property ServiceCenterNumber()Property ServiceCenterNumber() As String
InBlock.gif                
Get
InBlock.gif                    
Return SC_Number
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As String)
InBlock.gif                    
'Convert an ServiceCenterNumber to PDU Code
InBlock.gif
                    If InStr(Value, "+"Then
InBlock.gif                        SC_Number 
= "91"
InBlock.gif
                    Else
InBlock.gif                        SC_Number 
= "81"
InBlock.gif
                    End If
InBlock.gif
InBlock.gif                    Value 
= Mid(Value, 2)
InBlock.gif                    
Dim i As Integer
InBlock.gif                    
If (Value.Length Mod 2= 1 Then
InBlock.gif                        Value 
+= "F"
InBlock.gif
                    End If
InBlock.gif                    
For i = 1 To Value.Length Step 2
InBlock.gif                        SC_Number 
+= Swap(Mid(Value, i, 2))
InBlock.gif                    
Next
InBlock.gif                    SC_Number 
= ByteToHex((SC_Number.Length - 2/ 2 + 1+ SC_Number
InBlock.gif                
End Set
InBlock.gif
ExpandedSubBlockEnd.gif            
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Property TP_Status_Report_Request()Property TP_Status_Report_Request() As ENUM_TP_SRI
InBlock.gif                
Get
InBlock.gif                    
Return TP_SRR
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As ENUM_TP_SRI)
InBlock.gif                    TP_SRR 
= Value
InBlock.gif                
End Set
ExpandedSubBlockEnd.gif            
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Property TP_Message_Reference()Property TP_Message_Reference() As Integer
InBlock.gif                
Get
InBlock.gif                    
Return TP_MR
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As Integer)
InBlock.gif                    TP_MR 
= Value
InBlock.gif                
End Set
ExpandedSubBlockEnd.gif            
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Property TP_Destination_Address()Property TP_Destination_Address() As String
InBlock.gif                
Get
InBlock.gif                    
Return TP_DA
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As String)
InBlock.gif                    TP_DA 
= ""
InBlock.gif

InBlock.gif
                    If InStr(Value, "+"Then
InBlock.gif                        TP_DA 
+= "91"
InBlock.gif
                    Else
InBlock.gif                        TP_DA 
+= "81"
InBlock.gif
                    End If
InBlock.gif                    Value 
= Value.Replace("+""")
InBlock.gif                    TP_DA 
= Format(Value.Length, "X2"+ TP_DA
InBlock.gif                    
Dim i As Integer
InBlock.gif                    
If (Value.Length Mod 2= 1 Then
InBlock.gif                        Value 
+= "F"
InBlock.gif
                    End If
InBlock.gif                    
For i = 1 To Value.Length Step 2
InBlock.gif                        TP_DA 
+= Swap(Mid(Value, i, 2))
InBlock.gif                    
Next
InBlock.gif                
End Set
ExpandedSubBlockEnd.gif            
End Property

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Property TP_Data_Coding_Scheme()Property TP_Data_Coding_Scheme() As ENUM_TP_DCS
InBlock.gif                
Get
InBlock.gif                    
Return TP_DCS
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As ENUM_TP_DCS)
InBlock.gif                    TP_DCS 
= Value
InBlock.gif                
End Set
ExpandedSubBlockEnd.gif            
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Property TP_Validity_Period()Property TP_Validity_Period() As ENUM_TP_VALID_PERIOD
InBlock.gif                
Get
InBlock.gif                    
Return TP_VP
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As ENUM_TP_VALID_PERIOD)
InBlock.gif                    TP_VP 
= Value
InBlock.gif                
End Set
ExpandedSubBlockEnd.gif            
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Overridable Property TP_User_Data()Property TP_User_Data() As String
InBlock.gif                
Get
InBlock.gif                    
Return TP_UD
InBlock.gif                
End Get
InBlock.gif                
Set(ByVal Value As String)
InBlock.gif                    
Select Case TP_DCS
InBlock.gif                        
Case Is = ENUM_TP_DCS.DefaultAlphabet
InBlock.gif                            TP_UDL 
= Value.Length
InBlock.gif                            TP_UD 
= Encode7Bit(Value)
InBlock.gif                        
Case Is = ENUM_TP_DCS.UCS2
InBlock.gif                            TP_UDL 
= Value.Length * 2
InBlock.gif                            TP_UD 
= EncodeUCS2(Value)
InBlock.gif                        
Case Else
InBlock.gif                            TP_UD 
= Value
InBlock.gif                    
End Select
InBlock.gif                
End Set
ExpandedSubBlockEnd.gif            
End Property

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Functions#Region "Functions"
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Shared Function CheckForEncoding()Function CheckForEncoding(ByVal Content As StringAs SMS.ENUM_TP_DCS
InBlock.gif                
Dim i As Integer
InBlock.gif                
For i = 1 To Content.Length
InBlock.gif                    
If Asc(Mid(Content, i, 1)) < 0 Then
InBlock.gif                        
Return SMS.ENUM_TP_DCS.UCS2
InBlock.gif                    
End If
InBlock.gif                
Next
InBlock.gif                
Return SMS.ENUM_TP_DCS.DefaultAlphabet
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Overridable Function GetSMSPDUCode()Function GetSMSPDUCode() As String
InBlock.gif                
Dim PDUCode As String
InBlock.gif                
'Check User Data Length
InBlock.gif
                If TP_DCS = ENUM_TP_DCS.DefaultAlphabet Then
InBlock.gif                    
If TP_UD.Length > 280 Then Throw New Exception("User Data is TOO LONG for SMS")
InBlock.gif                
End If
InBlock.gif                
If TP_DCS = ENUM_TP_DCS.UCS2 Then
InBlock.gif                    
If TP_UD.Length > 280 Then Throw New Exception("User Data is TOO LONG for SMS")
InBlock.gif                
End If
InBlock.gif                
'Make PDUCode
InBlock.gif
                PDUCode = SC_Number
InBlock.gif                PDUCode 
+= FirstOctet()
InBlock.gif                PDUCode 
+= Format(TP_MR, "X2")
InBlock.gif                PDUCode 
+= TP_DA
InBlock.gif                PDUCode 
+= Format(TP_PID, "X2")
InBlock.gif                PDUCode 
+= Format(TP_DCS, "X2")
InBlock.gif                PDUCode 
+= Format(TP_VP, "X2")
InBlock.gif                PDUCode 
+= Format(TP_UDL, "X2")
InBlock.gif                PDUCode 
+= TP_UD
InBlock.gif                
Return PDUCode
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Overridable Function FirstOctet()Function FirstOctet() As String
InBlock.gif                
Return ByteToHex(TP_MTI + TP_VPF + TP_SRR + TP_UDHI)
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Shared Function ByteToHex()Function ByteToHex(ByVal aByte As ByteAs String
InBlock.gif                
Dim result As String
InBlock.gif                result 
= Format(aByte, "X2")
InBlock.gif                
Return result
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Enocode7Bit#Region "Enocode7Bit"
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Shared Function Encode7Bit()Function Encode7Bit(ByVal Content As StringAs String
InBlock.gif                
'Prepare
InBlock.gif
                Dim CharArray As Char() = Content.ToCharArray
InBlock.gif                
Dim c As Char
InBlock.gif                
Dim t As String
InBlock.gif                
For Each c In CharArray
InBlock.gif                    t 
= CharTo7Bits(c) + t
InBlock.gif                
Next
InBlock.gif                
'Add "0"
InBlock.gif
                Dim i As Integer
InBlock.gif                
If (t.Length Mod 8<> 0 Then
InBlock.gif                    
For i = 1 To 8 - (t.Length Mod 8)
InBlock.gif                        t 
= "0" + t
InBlock.gif                    
Next
InBlock.gif                
End If
InBlock.gif                
'Split into 8bits
InBlock.gif
                Dim result As String
InBlock.gif                
For i = t.Length - 8 To 0 Step -8
InBlock.gif                    result 
= result + BitsToHex(Mid(t, i + 18))
InBlock.gif                
Next
InBlock.gif                
Return result
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Shared Function BitsToHex()Function BitsToHex(ByVal Bits As StringAs String
InBlock.gif                
'Convert 8Bits to Hex String
InBlock.gif
                Dim i, v As Integer
InBlock.gif                
For i = 0 To Bits.Length - 1
InBlock.gif                    v 
= v + Val(Mid(Bits, i + 11)) * 2 ^ (7 - i)
InBlock.gif                
Next
InBlock.gif                
Dim result As String
InBlock.gif                result 
= Format(v, "X2")
InBlock.gif                
Return result
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Shared Function CharTo7Bits()Function CharTo7Bits(ByVal c As CharAs String
InBlock.gif                
If c = "@" Then Return "0000000"
InBlock.gif
                Dim Result As String
InBlock.gif                
Dim i As Integer
InBlock.gif                
For i = 0 To 6
InBlock.gif                    
If (Asc(c) And 2 ^ i) > 0 Then
InBlock.gif                        Result 
= "1" + Result
InBlock.gif                    
Else
InBlock.gif                        Result 
= "0" + Result
InBlock.gif                    
End If
InBlock.gif                
Next
InBlock.gif                
Return Result
ExpandedSubBlockEnd.gif            
End Function

ExpandedSubBlockEnd.gif
#End Region

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Shared Function EncodeUCS2()Function EncodeUCS2(ByVal Content As StringAs String
InBlock.gif                
Dim i, j, v As Integer
InBlock.gif                
Dim Result, t As String
InBlock.gif                
For i = 1 To Content.Length
InBlock.gif                    v 
= AscW(Mid(Content, i, 4))
InBlock.gif                    t 
= Format(v, "X4")
InBlock.gif                    Result 
+= t
InBlock.gif                
Next
InBlock.gif                
Return Result
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Shared Function Swap()Function Swap(ByRef TwoBitStr As StringAs String
InBlock.gif                
'Swap two bit like "EF" TO "FE"
InBlock.gif
                Dim c() As Char = TwoBitStr.ToCharArray
InBlock.gif                
Dim t As Char
InBlock.gif                t 
= c(0)
InBlock.gif                c(
0= c(1)
InBlock.gif                c(
1= t
InBlock.gif                
Return (c(0+ c(1)).ToString
ExpandedSubBlockEnd.gif            
End Function

ExpandedSubBlockEnd.gif
#End Region

ExpandedSubBlockEnd.gif        
End Class

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Public Class ConcatenatedShortMessageClass ConcatenatedShortMessage
InBlock.gif            
Inherits SMS
InBlock.gif            
Private TotalMessages As Integer
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Function GetEMSPDUCode()Function GetEMSPDUCode() As String()
InBlock.gif                
Select Case tp_dcs
InBlock.gif                    
Case ENUM_TP_DCS.UCS2
InBlock.gif                        TotalMessages 
= (TP_UD.Length / 4\ 66 + ((TP_UD.Length / 4 Mod 66= 0)
InBlock.gif                    
Case ENUM_TP_DCS.DefaultAlphabet
InBlock.gif                        TotalMessages 
= (tp_ud.Length \ 266- ((tp_ud.Length Mod 266= 0)
InBlock.gif                
End Select
InBlock.gif
InBlock.gif                
Dim Result(TotalMessages) As String
InBlock.gif                
Dim tmpTP_UD As String
InBlock.gif                
Dim i As Integer
InBlock.gif                TP_UDHI 
= 2 ^ 6
InBlock.gif                
Dim Reference As Integer = Rnd(1* 65536   '16bit Reference Number 'See 3GPP Document
InBlock.gif
                For i = 0 To TotalMessages
InBlock.gif                    
Select Case tp_dcs
InBlock.gif                        
Case ENUM_TP_DCS.UCS2
InBlock.gif                            tmpTP_UD 
= Mid(TP_UD, i * 66 * 4 + 166 * 4)
InBlock.gif                            
'When TP_UDL is odd, the max length of an Unicode string in PDU code is 66 Charactor.See [3GPP TS 23.040 V6.5.0 (2004-09] 9.2.3.24.1
InBlock.gif
                        Case ENUM_TP_DCS.DefaultAlphabet
InBlock.gif                            tmpTP_UD 
= Mid(tp_ud, i * 133 * 2 + 1133 * 2)
InBlock.gif                    
End Select
InBlock.gif                    Result(i) 
= SC_Number
InBlock.gif                    Result(i) 
+= FirstOctet()
InBlock.gif                    Result(i) 
+= Format(TP_MR, "X2")
InBlock.gif                    
'Next segement TP_MR must be increased
InBlock.gif
                    'TP_MR += 1
InBlock.gif
                    Result(i) += TP_DA
InBlock.gif                    Result(i) 
+= Format(TP_PID, "X2")
InBlock.gif                    Result(i) 
+= Format(TP_DCS, "X2")
InBlock.gif                    Result(i) 
+= Format(TP_VP, "X2")
InBlock.gif                    
If tp_dcs = ENUM_TP_DCS.UCS2 Then
InBlock.gif                        TP_UDL 
= tmpTP_UD.Length / 2 + 6 + 1 '6:IE
InBlock.gif
                    End If
InBlock.gif                    
If tp_dcs = ENUM_TP_DCS.DefaultAlphabet Then
InBlock.gif                        TP_UDL 
= Fix((tmpTP_UD.Length + 7 * 2* 4 / 7)   '6:length of IE
InBlock.gif
                        ''#################################
InBlock.gif
                        ''still problem here:
InBlock.gif
                        ''when the charcter is several times of 7 of the last message, tp_udl will not correct!
InBlock.gif
                        ''to correct this problem i write some code below. that's may not perfect solution.
InBlock.gif
                        ''#################################
InBlock.gif
                        'If i = TotalMessages And ((tmpTP_UD.Length Mod 14) = 0) Then
InBlock.gif
                        '    tp_udl -= 1
InBlock.gif
                        'End If
InBlock.gif
                    End If
InBlock.gif                    Result(i) 
+= Format(TP_UDL, "X2")
InBlock.gif                    Result(i) 
+= "060804" 'TP_UDHL and some of Concatenated message
InBlock.gif
                    Result(i) += Format(Reference, "X4")
InBlock.gif                    Result(i) 
+= Format(TotalMessages + 1"X2")
InBlock.gif                    Result(i) 
+= Format(i + 1"X2")
InBlock.gif                    Result(i) 
+= tmpTP_UD
InBlock.gif                
Next
InBlock.gif                
Return Result
ExpandedSubBlockEnd.gif            
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
Public Overrides Function FirstOctet()Function FirstOctet() As String
InBlock.gif                
Return ByteToHex(TP_MTI + TP_VPF + TP_SRR + TP_UDHI)
ExpandedSubBlockEnd.gif            
End Function

ExpandedSubBlockEnd.gif        
End Class

ExpandedSubBlockEnd.gif    
End Namespace

ExpandedBlockEnd.gif
End Namespace

None.gif
None.gif

转载于:https://www.cnblogs.com/hesicong/archive/2005/08/03/207043.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值