更新时间:2023-11-15 19:45:52
基本上,你有两个函数来获取一个字节数组(你称之为'十六进制字符')到一个字符串,反之亦然:
Basically, you have two functions to get a byte array (what you call 'hexadecimal characters') to a string, and vice-versa:
using System.Text;
// Converting an UTF8 string to a byte array
string input = "whatever";
byte[] bytes = Encoding.UTF8.GetBytes(input);
// Converting a byte array back to its original string representation
string result = Encoding.UTF8.GetString(bytes);
一旦你有一个字节数组,以十六进制形式显示它的任务是微不足道的。以下是扩展方法中的示例实现:
Once you have a byte array, the task to display it in its hexadecimal form is trivial. Here's an example implementation in an extension method:
using System.Text;
public static string ToHexadecimalRepresentation(this byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.Length << 1);
foreach (byte b in bytes) {
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}
希望这有帮助。
Hope this helps.