- A+
所属分类:.NET技术
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp4 { class Program { static void Main(string[] args) { byte[] byt1 = { 0x01, 0x11, 0x21,0xff ,0b0001_0001,0b0001_1111};//定义并初始化8位无符号字节数组 StringBuilder sNeed = new StringBuilder(); foreach (var item in byt1) Console.Write(item + "**");//遍历并输出每个字节对应的十进制数值 Console.WriteLine("r"); Console.WriteLine(BitConverter.ToString(byt1));//将字节数组转换为字符串并输出 Console.WriteLine(BitConverter.ToString(byt1).Replace("-",""));//去掉连接符 sNeed.Append(BitConverter.ToString(byt1).Replace("-", "")); Console.WriteLine(sNeed.ToString()); Console.WriteLine(BitConverter.ToString(strToToHexByte(sNeed.ToString()))); Console.ReadKey(); } private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace("-", ""); if ((hexString.Length % 2) != 0) hexString += "20"; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } } }
如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !