【问题标题】:Send all bytes from richTextBox从richTextBox 发送所有字节
【发布时间】:2020-02-19 15:38:12
【问题描述】:

我有一个richTextBox 和字节数组。

byte[] message = Encoding.Default.GetBytes(richTextBox1.Text);

byte[] bytesToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (MESSAGE SHOULD BE HERE), 0x00 };

我该怎么做?

【问题讨论】:

标签: c# arrays richtextbox


【解决方案1】:
    static byte[] SomeMethod(string value, Encoding encoding)
    {
        // create an array with the space needed for the value and the zeros
        var bytes = new byte[encoding.GetByteCount(value) + 9];
        // encode the data into the array starting at position 8
        encoding.GetBytes(value, 0, value.Length, bytes, 8);
        // and we're done
        return bytes;
    }
    var bytesToSend = SomeMethod(richTextBox1.Text, Encoding.Default);
    // (but probably not Encoding.Default - that is almost always wrong)

【讨论】:

  • 谢谢!但是如果我想发送一些不同的字节而不是零呢?
  • @tony then ... 覆盖您要更改的字节?
  • byte[] bytesToSend = new byte[] { 0x08, 0x08, 0xAB, 0xFE, 0x00, 0x00, 0x00, 0x00, (MESSAGE SHOULD BE HERE), 0x00 }
  • @tony 您可以一个一个地手动覆盖它们,或者您可以创建一个静态字节 [] 字段,即这些字节并使用内存复制...坦率地说,在这种情况下我d 可能只是手动操作:bytes[0] = bytes[1] = 0x08; bytes[2] = 0xAB; bytes[3] = 0xFE; ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
相关资源
最近更新 更多