在 Visual Basic 2010 中将 ucs-2 转换为 utf-8 [英] convert ucs-2 to utf-8 in visual basic 2010

查看:66
本文介绍了在 Visual Basic 2010 中将 ucs-2 转换为 utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用visual baisc 2010 和usb 调制解调器通过SerialPortAT+CUSD=1"发送命令ussd",我的问题是在接收结果得到ucs-2 时出现这样的问题

Hello I used visual baisc 2010 and usb modem to sent at commands " ussd " by SerialPort "AT+CUSD=1" my problem when recive result get ucs-2 like this

+ CUSD:0 00430075007200720065006E007400540069006D0065002000690073003A002000320031002D004A0055004C002D0032003000310038002000310036003A00320036",72

+CUSD: 0,"00430075007200720065006E007400540069006D0065002000690073003A002000320031002D004A0055004C002D0032003000310038002000310036003A00320036",72

如何转换为 utf-8

how i can convert to utf-8

推荐答案

看起来像那个字符串,因为它的组成,在 BigEndianUnicode 格式.
此编码格式可从 .Net FW 3.5+/VS 2008 获得.
不过,使用中的 .Net 版本比 Visual Studio 版本更重要.

It looks like that string, because of its composition, is in BigEndianUnicode format.
This encoding format is available from .Net FW 3.5+ / VS 2008.
The .Net version in use is more important than the Visual Studio version, though.

那么,让我们尝试解析这个字符串,看看结果如何.

So, let's try to parse this string and see what comes out of it.

Dim input As String = [SerialPortOutput]

input = input.Replace(ChrW(34), "")
Dim ucs2 As String = input.Split(","c)(1)

Dim HexBytes As List(Of Byte) = New List(Of Byte)()
For i As Integer = 0 To ucs2.Length - 1 Step 2
    HexBytes.Add(Byte.Parse(ucs2.Substring(i, 2), NumberStyles.HexNumber))
Next

现在,将字节列表从 BigEndianUnicode 转换为标准的 .Net 字符串.

Now, transform the List of bytes from BigEndianUnicode to a standard .Net string.

Dim output As String = Encoding.BigEndianUnicode.GetString(HexBytes.ToArray())

输出字符串如下:

The output string reads:

当前时间是:21-JUL-2018 16:26"

"CurrentTime is: 21-JUL-2018 16:26"

要将其转换为 UTF8,如果确实需要(可能是 Internet 传输),请获取编码的字节数组:

To convert it to UTF8, if really needed (Internet transfer, maybe), get the encoded array of bytes:

Dim UTF8Bytes As Byte() = Encoding.UTF8.GetBytes(output)

这篇关于在 Visual Basic 2010 中将 ucs-2 转换为 utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆