C#-字节数组到十六进制字符串
本文关键字:十六进制 字符串 数组 字节 字节数 C#- | 更新日期: 2023-09-27 18:19:56
我正在为《现代战争2》制作一名教练。我遇到的问题是将十六进制转换为字符串,我对此还很陌生,但在尝试任何东西之前,我确实会四处看看。在发布这个问题之前,我也环顾四周。这是我的代码:
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("-", "");
}
我得到的返回值是:330400000100100100000000000000
但我需要它来返回这个:110000100000433
有什么建议吗?
我认为这是一个小终结点对大终结点的问题。请尝试以下操作:
public static string ByteArrayToString(byte[] ba)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(ba);
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
参考文献:
- BitConverter类
- Endian秩序随笔
为什么不使用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");
}