byte[] bs = new byte[32]; Random random = new Random(); random.NextBytes(bs); //给字节数组填充随字节 string hex = BitConverter.ToString(bs); //将字节数组转成十六进制字符串,默认-分割 Console.WriteLine(hex); var bs2 = Array.ConvertAll(hex.Split('-'), p => Convert.ToByte(p, 16)); //将-分割的十六进制字符串转成字节数组 Console.WriteLine(bs.SequenceEqual(bs2)); //对比转换后的数据是否相等 int num = 1234567890; Console.WriteLine("0x" + num.ToString("x")); //将数值转成十六进制,x小写时得到的字符串字母小写 var hex2 = num.ToString("X"); Console.WriteLine("0x" + hex2); //将数值转成十六进制,x大写时得到的字符串字母大写 Console.WriteLine("0x" + num.ToString("X10")); //将数据转成十六进制,不足10位,补零,超过不影响 var num2 = Convert.ToInt32(hex2, 16); //将十六进制字符串转成十进制数值 Console.WriteLine(num == num2); //对比
标签:十六进制,Console,字节,C#,num,WriteLine,字符串 From: https://www.cnblogs.com/luludongxu/p/17967066