vb.net:使用Unicode将字节数组编码成字符串 [英] vb.net: Encoding byte array into string using Unicode
问题描述
我将这个Bytes序列存储到一个字节数组中,我在VB.NET中定义如下:
Dim frame()作为Byte
所以上述数组中的每个元素都在[0-255]
我想将这些字节中的每一个字节编码为ASCII,UTF-8和Unicode,以便遍历字节数组(frame)并执行以下代码片段代码,具体取决于案例:
ASCII :
对于idxByte As Integer = 0 To Me.frame.Length - 1
txtRefs(idxByte).Text = Encoding.ASCII.GetString(String.Format(< {0}>,Encoding.GetString ,idxByte,1))
下一个
注意:txtRefs是一组文本框,其长度与框架相同。
另外两个编码类似:
UTF -8
对于idxByte As Integer = 0 To Me.frame.Length - 1
txtRefs (idxByte).Text = Encod ing.UTF8.GetString(String.Format(< {0}>,Encoding.GetString(frame,idxByte,1))
下一个
Unicode :
对于idxByte As Integer = 0 To Me.frame.Length - 1
txtRefs(idxByte).Text = Encoding.Unicode.GetString(String.Format(< {0}>,Encoding.GetString(frame,idxByte ,1))
下一个
ASCII和UTF8编码似乎可以,但Unicode编码似乎不工作,也可能我不了解Unicode编码功能...
对于unicode,我得到以下 result 通过执行上述循环。这是否正确?
Encoding.Unicode
是UTF-16 LE,所以它需要两个字节来给出正确的结果。例如
Dim input()As Byte = {65,0}
Dim x = Encoding.Unicode.GetString(输入,0,1)
Dim y = Encoding.Unicode.GetString(input,0,2)
Console.WriteLine(x = {0},y = {1},x,y )
x = ,y = A
但是,如果您的输入是每个字符的单字节,您可能不希望从输入数组传递两个字节。您可能想要创建一个零第二个字节的新输入数组:
Dim input()As Byte = {65,0
Dim x = Encoding.Unicode.GetString(input,0,1)
Dim y = Encoding.Unicode.GetString(input,0,2)
Dim z = Encoding.Unicode。 GetString(New Byte(){input(0),0})
Console.WriteLine(x = {0},y = {1},z = {2},x,y,z)
x = ,y = A,z = A
很难说,不知道你的输入和所需的输出。
I am reading RAW data from a source. This raw data is a sequence of Bytes. I store this sequence of Bytes into an array of Bytes that I define as following in VB.NET:
Dim frame() as Byte
so each element in the above array is in the range [0-255].
I want to encode each of these bytes into ASCII, UTF-8 and Unicode so I iterate over the byte array (frame) and perform below snippet code depending on the case:
ASCII:
For idxByte As Integer = 0 To Me.frame.Length - 1
txtRefs(idxByte).Text = Encoding.ASCII.GetString(String.Format("<{0}>", Encoding.GetString(frame, idxByte, 1))
Next
Note: txtRefs is an array of textboxes, and its length is the same as frame.
And similar for the other two encodings:
UTF-8:
For idxByte As Integer = 0 To Me.frame.Length - 1
txtRefs(idxByte).Text = Encoding.UTF8.GetString(String.Format("<{0}>", Encoding.GetString(frame, idxByte, 1))
Next
Unicode:
For idxByte As Integer = 0 To Me.frame.Length - 1
txtRefs(idxByte).Text = Encoding.Unicode.GetString(String.Format("<{0}>", Encoding.GetString(frame, idxByte, 1))
Next
ASCII and UTF8 encoding seems ok, but Unicode encoding seems it is not working or maybe I am not understanding Unicode encoding functionality at all...
For unicode I get below result by executing above loop. Is this correct?
Encoding.Unicode
is UTF-16 LE, so it needs two bytes to give the correct results. e.g.
Dim input() As Byte = { 65, 0 }
Dim x = Encoding.Unicode.GetString(input, 0, 1)
Dim y = Encoding.Unicode.GetString(input, 0, 2)
Console.WriteLine("x={0}, y={1}", x, y)
x=�, y=A
However, if your input is single byte per character you probably don't want to just pass two bytes from your input array. You may want to create a new input array with a zero second byte:
Dim input() As Byte = { 65, 0 }
Dim x = Encoding.Unicode.GetString(input, 0, 1)
Dim y = Encoding.Unicode.GetString(input, 0, 2)
Dim z = Encoding.Unicode.GetString(New Byte() { input(0), 0 })
Console.WriteLine("x={0}, y={1}, z={2}", x, y, z)
x=�, y=A, z=A
Hard to tell without knowing your input and desired output.
这篇关于vb.net:使用Unicode将字节数组编码成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!