繁体   English   中英

将字节添加到字节数组C#

[英]Add bytes to byte array C#

如何将以下字节添加到ac#字节数组?

00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00

这有意义吗?

public void updateBytes(string exeName, int value)
{
    long baseaddress = GetBaseAddress(exeName, exeName + ".exe");
    long pointer = GetPointerAddress(baseaddress + 0x04105320, new int[] { value });

    byte[] intBytes = "00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00";

    WriteBytes(pointer, intBytes);
}

我将不胜感激

在我看来,您并不是真的要向数组中添加项目,而是要创建/初始化具有给定值的数组。

您可以使用以下语法来初始化数组:

byte[] intBytes = {0,0,0,4,5,1,1 /* ... */ };

如果要将string解析为字节数组( byte[] ),则可以使用Linq

string source = "00 00 00 00 00 00 00 00 04 05...";

byte[] intBytes = source
  .Split(' ')
  .Select(item => Convert.ToByte(item, 16))
  .ToArray();

好的,首先您要有一个string然后将其分配给byte[] ,这是一个很大的诺诺。 如果您不能手动将其更改为byte[] (由于某些怪异的协议或其他原因),则可以执行以下操作:

// assign bytes to string
string meBytes = "00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00";
// split them by spaces
string[] hexBytes = meBytes.Split(new char[] { (char)0x20 });
// extract bytes
byte[] bytes = meBytes.Select(x => Convert.ToByte(x, 16)).ToArray();
// now you can write them into stream
WriteBytes(pointer, bytes);

您甚至可以在两行中执行此操作:

string meBytes = "00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00";
WriteBytes(pointer, meBytes.Split(new char[] { (char)0x20 }).Select(x => Convert.ToByte(x, 16)).ToArray());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM