0

How could I add the following bytes to a c# 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

3
  • This doesn't even compile as of yet. byte[] != string Commented Dec 15, 2016 at 14:05
  • Possible duplicate of c# how to add byte to byte array Commented Dec 15, 2016 at 14:05
  • It is better to use a list object which has an add method than to use an array. Commented Dec 15, 2016 at 14:08

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

2 Comments

meBytes.Split(0x20) doesn't compile: 0x20 is of type int, when char is expected; 0x20 is magic number antipattern (what does it stand for?) when ' ' (space) is evident
@DmitryBychenko Thank you for pointing that. Fixed it. And 0x20 ( as everyone knows ) is just a space character and it is more convenient ( for me and people i know ) to write bytes/chars in hex format.
1

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 /* ... */ };

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.