C# byte[] 轉換16進制字符串
阿新 • • 發佈:2018-02-23
int32 tostring splay gets isp 方式 line -a ++
View Code
View Code
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);
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);
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);View Code8 Console.WriteLine(str);
C# byte[] 轉換16進制字符串