C# byte[] 转换16进制字符串

StringBuilder方式

var str = DateTime.Now.ToString();
var encode = Encoding.UTF8;
var bytes = encode.GetBytes(str);
StringBuilder ret = new StringBuilder();
foreach (byte b in bytes)
{
//{0:X2} 大写
ret.AppendFormat("{0:x2}", b);
}
var hex = ret.ToString();
Console.WriteLine(hex);


16进制字符串转换 byte[]

var inputByteArray = new byte[hex.Length / 2];
for (var x = 0; x < inputByteArray.Length; x++)
{
    var i = Convert.ToInt32(hex.Substring(x * 2, 2), 16);
    inputByteArray[x] = (byte)i;
}
str = encode.GetString(inputByteArray);
Console.WriteLine(str);

//网络素材仅限收藏 方便学习

C# byte[] 转换16进制字符串

上一篇:C# MD5加密 2位宽度导致少了个0/多了个0 Base64加密


下一篇:Win10 LTSC 激活方法| Win10专业版(永久激活)长期服务版 LTSC 2019密钥