vb 接收中文短信
以下代码可以接收英文或者数字的短信,如果短信内容有汉字和英文什么都不显示。只显示一大堆进制。我网上找一些进制转换代码来转换,只能转换发信人号码,时间等等数据,无法转换汉字。
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim MyData
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
Sleep (2000)
Dim s As String
s = strData
If Len(s) Mod 2 <> 0 Then s = "0" & s
Dim arr() As Byte
ReDim arr(Len(s) \ 2 - 1) As Byte
Dim xx As Long
For xx = 0 To Len(s) \ 2 - 1
arr(xx) = "&h" & Mid(s, (xx * 2) + 1, 2)
Next
End Select
MyData = UTF8_Decode(arr)
Text3 = MyData + vbCr
End Sub
Public Function UTF8_Decode(ByRef bUTF8() As Byte) As String
'UTF-8 解码
Dim lRet As Long
Dim lLen As Long
Dim lBufferSize As Long
Dim sBuffer As String
Dim bBuffer() As Byte
lLen = UBound(bUTF8) + 1
If lLen = 0 Then Exit Function
lBufferSize = lLen * 2
sBuffer = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bUTF8(0)), lLen, StrPtr(sBuffer), lBufferSize)
If lRet <> 0 Then
sBuffer = Left(sBuffer, lRet)
End If
UTF8_Decode = sBuffer
End Function
接收的《你好》这字《4F60597D》这样显示的.两天了搞这个好闷呀 --------------------编程问答-------------------- 我发现这些
READ","13899080000",,"12/05/15,02:09:40+32",161,36,0,8,"+8613010969500",145,2
4F60597D
(这是《你好》)
READ","13899080000",,"12/05/14,16:58:43+32",161,36,0,0,"+8613010969500",145,5
Nihao
READ","13899080000",,"12/05/14,19:43:06+32",161,36,0,8,"+8613010969500",145,12
004E006900680061006FFF014F60597DFF01003100320033
(这是《你好!Nihao!123》)
",161,36,0,8,"+ 这里面的《8》好像是进制吧 因为它只在中文短信时才会有,英文短信时《0》。
145,12 这里面的《12》是代表短信长度了。
请我大家这些数据怎么处理呀?要是中文信息八进制的话怎么会还有《F,D》之类的呢 网上不是说八进制最大数是7嘛?
还有一个问题 ",,"12/05/14,19:43:06+32",这后面的+32是什么意思呢?所以短信都是这样32了 --------------------编程问答-------------------- "004E006900680061006FFF014F60597DFF01003100320033"
解码后:"Nihao!你好!123"
Escape编码。 --------------------编程问答-------------------- 我也需要接短信串口编程,代码分享下好吗 --------------------编程问答-------------------- Escape 怎么用呀?
百度上说要说明 Declare Function Escape Lib "gdi32" (ByVal hdc As Long, ByVal nEscape As Long, ByVal nCount As Long, ByVal lpInData As String, lpOutData As Any) As Long
。
但是vb不允许这些,怎么办呀 --------------------编程问答-------------------- 终于问题解决了。
本来收到的那些数据可以用chrw(&h)方式转中文了。
004E006900680061006FFF014F60597DFF01003100320033
这数据我本来2个分一组的,后来发现4个一组才能转换汉字了。
比如:004E 0069 0068 0061 006F FF01 4F60 597D FF01 0031 0032 0033
也许我第一次接触这些数据所以对我来说那么复杂吧
请高手别取笑!
说实话我到现在还不明白004E006900680061006FFF014F60597DFF01003100320033这些数据是什么格式什么进制的
Public Function HenSi(mNR As String)
Dim i As Long
For i = 0 To Len(mNR) / 4 - 1
Ass = Mid(mNR, (i * 4) + 2, 4)
HenSi = HenSi + ChrW("&h" + Ass)
Next
End Function
补充:VB , 基础类