代码实现
/// <summary>
/// bytes 转 string
/// </summary>
static class HexToString
{
public static string ToHexString(byte[] bytes)
{
return ToHexString(bytes, true);
}
public static string ToHexString(byte[] bytes, bool space)
{
return ToHexString(bytes, bytes.Length, space);
}
public static string ToHexString(byte[] bytes, int length, bool space)
{
string strFill = space ? " " : "";
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < length; i++)
{
strB.Append(bytes[i].ToString("X2") + strFill);
}
hexString = strB.ToString();
}
hexString = hexString.Trim();
return hexString;
}
}
调用
private void button4_Click(object sender, EventArgs e)
{
byte[] bytes = new byte[4];
bytes[0] = 0xab;
bytes[1] = 0x02;
bytes[2] = 0x34;
bytes[3] = 0x56;
richTextBox1.AppendText(HexToString.ToHexString(bytes));
}
参考链接
特此记录
anlog
2025年6月13日
8万+

被折叠的 条评论
为什么被折叠?



