1

I am making a trainer for Modern Warfare 2. The problem I am having is the conversion of hex to string, I am fairly new to this but I do look around before I try anything. I have also looked around before posting this question. Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    int xbytesRead = 0;
    byte[] myXuid = new byte[15];
    ReadProcessMemory((int)processHandle, xuidADR, myXuid, myXuid.Length, ref xbytesRead);
    string xuid = ByteArrayToString(myXuid);
    textBox2.Text = xuid;
}

public static string ByteArrayToString(byte[] ba)
{
    string hex = BitConverter.ToString(ba);
    return hex.Replace("-", "");
}

The return value I am getting is: 330400000100100100000000000000

But I need it to return this: 110000100000433

Any suggestions?

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
Seann
  • 11
  • 1
  • 2
  • look here http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa – Greenonion Aug 29 '14 at 13:42
  • 1
    @Greenonion: Where do you think OP got that code from? Can't imagine many people use `ba` as a parameter name – musefan Aug 29 '14 at 13:44
  • It seems a Little Endian vs Big Endian issue. http://people.cs.umass.edu/~verts/cs32/endian.html – Wagner DosAnjos Aug 29 '14 at 13:46
  • @Greenonion have already seen that topic. Thats where i got the conversion from but i'm puzzled on getting it from :330400000100100100000000000000 to : 110000100000433 – Seann Aug 29 '14 at 13:46
  • 1
    FYI, the modern warfare bit has no bearing on the question. It doesn't need to be mentioned – musefan Aug 29 '14 at 13:47
  • @musefan sorry I thought it would be polite to give a bit of information on what I am trying to achieve. – Seann Aug 29 '14 at 13:48

2 Answers2

0

I think this is a Little-Endian vs Big-Endian issue. Please try the following:

public static string ByteArrayToString(byte[] ba)
{
    if (BitConverter.IsLittleEndian)
         Array.Reverse(ba);

    string hex = BitConverter.ToString(ba);
    return hex.Replace("-", "");
}

References:

Wagner DosAnjos
  • 6,304
  • 1
  • 15
  • 29
0

Why dont use int?

private void button1_Click(object sender, EventArgs e)
{
int xbytesRead = 0;
byte[] myXuid = new byte[15];
ReadProcessMemory((int)processHandle, xuidADR, myXuid, myXuid.Length, ref xbytesRead);
string xuid = ByteArrayToString(myXuid);
textBox2.Text = xuid;
}

public static string ByteArrayToString(byte[] ba)
{
  int hex=0;
  for(i=0;i<ba.Length;i++)
     hex+=Convert.ToInt(ba[i])*Math.Pow(256,i)
  return hex.ToString("X");
}
Boz
  • 51
  • 1
  • 11