简体   繁体   English

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

[英]Add bytes to byte array C#

How could I add the following bytes to ac# byte array? 如何将以下字节添加到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

Does this make sense? 这有意义吗?

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);
}

I would appreciate any kind of help 我将不胜感激

It seems to me that you don't really want to add items to an array but create/initialize an array with given values. 在我看来,您并不是真的要向数组中添加项目,而是要创建/初始化具有给定值的数组。

You can use the following syntax to initialize an array: 您可以使用以下语法来初始化数组:

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

if you want parse string into array of byte ( byte[] ) you can use Linq : 如果要将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();

Okay, so first of all you have a string and you want to assign this to byte[] which is a big nono. 好的,首先您要有一个string然后将其分配给byte[] ,这是一个很大的诺诺。 If you cannot change this to byte[] by hand ( because of some weird protocol or something ) you can do this like that : 如果您不能手动将其更改为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);

You can even do this in two lines : 您甚至可以在两行中执行此操作:

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