简体   繁体   English

C#-十六进制字符串的字节数组

[英]C# - Byte array to Hex string

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. 我正在为《现代战争2》做一名培训师。我遇到的问题是将十六进制转换为字符串,对此我还很陌生,但是在尝试任何操作之前,我确实四处张望。 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 我得到的返回值是: 330400000100100100000000000000

But I need it to return this: 110000100000433 但我需要它返回此: 110000100000433

Any suggestions? 有什么建议么?

I think this is a Little-Endian vs Big-Endian issue. 我认为这是Little-Endian与Big-Endian问题。 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: 参考文献:

Why dont use int? 为什么不使用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");
}

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

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