简体   繁体   中英

Add bytes to byte array C#

How could I add the following bytes to ac# byte array?

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 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. If you cannot change this to byte[] by hand ( because of some weird protocol or something ) you can do this like that :

// 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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