将单个字节附加到 VB.NET 中的字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17728669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:18:32  来源:igfitidea点击:

Append single byte to byte array in VB.NET

vb.netappendbytearray

提问by Toby

I have a string that I'd like to get the ASCII byte representation of, then add two more single ASCII bytes to the end of.

我有一个字符串,我想获取其 ASCII 字节表示,然后在末尾添加另外两个单个 ASCII 字节。

What is the simplest way to do this? From my searching using Google, it seems that VB's append methods all append only strings and arrays, not characters or bytes... Is this the case?

什么是最简单的方法来做到这一点?从我使用谷歌的搜索来看,似乎 VB 的 append 方法都只附加字符串和数组,而不是字符或字节......是这种情况吗?

For example,

例如,

Dim byte1 As Byte = &H4
Dim byte2 As Byte = &HA

Dim array() As Byte = Encoding.ASCII.GetBytes(MyTextBox.Text) + byte1 + byte2

Then if "ABC" is entered in the text box, the array should end up holding hex 41, 42, 43, 04, 0A.

然后,如果在文本框中输入“ABC”,则数组最终应包含 hex 41, 42, 43, 04, 0A

回答by dbasnett

Try this

尝试这个

    Dim byte1 As Byte = &H4
    Dim byte2 As Byte = &HA

    Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text & Chr(byte1) & Chr(byte2))

回答by Anderson Nunes

You can concatenate the two values with:

您可以将两个值连接在一起:

Dim array() as Byte = {byte1, byte2}