HOWTO:使用不同的编码将单个字节解码为字符串

时间:2016-03-09 02:24:25

标签: vb.net unicode encoding utf-8 ascii

我想将单个字节表示为不同类型的编码,如unicode,UTF-8,ASCII和操作系统的当前ANSI代码页,那么我怎样才能在vb.net?

例如,假设我有一个字节数组,其数字从0到255,长度为256:

Dim byteArray(255) As Byte

byteArray(0)=0
byteArray(1)=1
byteArray(2)=2
...
byteArray(255)=255

如果下面的代码能够实现我想要的东西,我完全不确定。任何人都可以确认我是否正确?我不是这方面的专家......

Dim unicode_string as String
Dim utf8_string as String
Dim ascii_string as String
Dim default_string as String

For i as Integer = 0 to byteArray.length - 1
   ' As Unicode is UTF-16, uses 2 bytes, we construct a new byte array with its second byte to zero.
   unicode_string = Encoding.Unicode.GetString(New Byte() {byteArray(i), 0})
   utf8_string = Encoding.UTF8.GetString(byteArray, i, 1)
   ascii_string = Encoding.ASCII.GetString(byteArray, i, 1)
   default_string = Encoding.Default.GetString(byteArray, i, 1)

   Console.WriteLine("{0} {1} {2} {3}", unicode_string, utf8_string, ascii_string, default_string)
Next

0 个答案:

没有答案