PDU格式短消息转换源代码
搜集-乐信 来源-【乐信工作室】
Public Function txt2pdu(ByVal stxt As String) As String
For Y = 1 To Len(stxt)
X = Dec2Bin(Asc(Mid(stxt, Y, 1)))
'If X = "100000" Then X = "0100000"
Text = X & Text
Next Y
'Debug.Print Text
l = Len(Text)
s = l - 7
out = Bin2Hex(Mid(Text, s, 8))
Do Until s <= 8
s = s - 8
out = out & Bin2Hex(Mid(Text, s, 8))
Loop
out = out & Bin2Hex(Mid(Text, 1, s - 1))
'Debug.Print out
X = Hex(Len(stxt))
If Len(X) = 1 Then
X = "0" & X
End If
'Debug.Print X & out
txt2pdu = X & out
End Function
Public Function Dec2Bin(ByVal nDec As Integer) As String
'This function is the same then Hex2Bin,
' but it has been copied to speed up proce
' ss
Dim i As Integer
Dim j As Integer
Dim sHex As String
Const HexChar As String = "0123456789ABCDEF"
sHex = Hex(nDec) 'That the only part that is different
For i = 1 To Len(sHex)
nDec = InStr(1, HexChar, Mid(sHex, i, 1)) - 1
For j = 3 To 0 Step -1
Dec2Bin = Dec2Bin & nDec \ 2 ^ j
nDec = nDec Mod 2 ^ j
Next j
Next i
'Remove the first unused 0
i = InStr(1, Dec2Bin, "1")
If i <> 0 Then Dec2Bin = Mid(Dec2Bin, i)
If Len(Dec2Bin) = 6 Then Dec2Bin = "0" & Dec2Bin
End Function
Public Function Bin2Hex(ByVal sBin As String) As String
Dim i As Integer
Dim nDec As Long
sBin = String(4 - Len(sBin) Mod 4, "0") & sBin 'Add zero to complete Byte
For i = 1 To Len(sBin)
nDec = nDec + CInt(Mid(sBin, Len(sBin) - i + 1, 1)) * 2 ^ (i - 1)
Next i
Bin2Hex = Hex(nDec)
If Len(Bin2Hex) Mod 2 = 1 Then Bin2Hex = "0" & Bin2Hex
End Function
评论0