GSM下Unicode和中文字符转码
GSM的调试,通常涉及中文字符的转码UNICODE和ASCII。发送中文时,需要先把中文和SIM号码转换为UNICODE发送出去,然后接收到UNICODE的字符,需要转换为中文内容。这部分代码需要做在上位机里。因为下位机的UNICODE支持不是很好。具体代码如下:
Public Function Unicode2String(ByVal unicode As String) As String ' unicode编码转中文 Dim resultstr As String = "" Dim slen As Integer = unicode.Length If slen Mod 4 <> 0 Then Return resultstr End If Dim blen As Integer = unicode.Length / 4 Dim slist(0 To blen - 1) As String For i = 0 To blen - 1 slist(i) = unicode.Substring(4 * i, 4) Next For i = 0 To slist.Length - 1 resultstr += ChrW(Integer.Parse(slist(i), System.Globalization.NumberStyles.HexNumber)) ' 必须用 chrW Next Return resultstr End FunctionPublic Function Str2Unicode(ByVal txt As String) As String '中文转Unicode Dim a() As String Dim str As String Dim i As Integer For i = 0 To Len(txt) - 1 On Error Resume Next str = Mid(txt, i + 1, 1) 'If isChinese(str) = True Then '//是中文 Str2Unicode = Str2Unicode & StrDup(4 - Len(Hex(AscW(str))), "0") & Hex(AscW(str)) 'Else '//不是中文 'Str2Unicode = Str2Unicode & str 'End If Next End Function