1. 程式人生 > >C# byte[] 轉換16進制字符串

C# byte[] 轉換16進制字符串

int32 tostring splay gets isp 方式 line -a ++

1.byte[] 轉換16進制字符串

1.1 BitConverter方式

技術分享圖片
1             var str = DateTime.Now.ToString();
2             var encode = Encoding.UTF8;
3             var bytes = encode.GetBytes(str);
4             var hex = BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower();
5             Console.WriteLine(hex);
View Code

1.2 StringBuilder方式

技術分享圖片
1             var str = DateTime.Now.ToString();
2             var encode = Encoding.UTF8;
3             var bytes = encode.GetBytes(str);
4             var hex = BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower();
5             Console.WriteLine(hex);
View Code

2.16進制字符串轉換 byte[]

技術分享圖片
1 var inputByteArray = new byte[hex.Length / 2];
2             for (var x = 0; x < inputByteArray.Length; x++)
3             {
4                 var i = Convert.ToInt32(hex.Substring(x * 2, 2), 16);
5                 inputByteArray[x] = (byte)i;
6             }
7             str = encode.GetString(inputByteArray);
8 Console.WriteLine(str);
View Code

C# byte[] 轉換16進制字符串