首页 > 代码库 > 数据按照十六进制文本输入,可自动调节输出宽度
数据按照十六进制文本输入,可自动调节输出宽度
public static string PrintHex(byte[] data, int rowLength) { if (rowLength % 2 == 1) throw new ArgumentException("必须是偶数!"); var buffer = new StringBuilder(); string segmentNumber = ""; string bytes = ""; string ascii = ""; for (int i = 1; i <= data.Length; i++) { bytes += (data[i - 1].ToString("X2")) + " "; if (data[i - 1] < 0x21 || data[i - 1] > 0x7e) { ascii += "."; } else { ascii += Encoding.ASCII.GetString(new byte[1] { data[i - 1] }); } if (i % rowLength != 0 && i % (rowLength / 2) == 0) { bytes += " "; ascii += " "; } if (i % rowLength == 0) { segmentNumber = ((((i - rowLength) / rowLength) * rowLength).ToString("X4")); buffer.AppendLine(segmentNumber + " " + bytes + " " + ascii); bytes = ""; ascii = ""; continue; } if (i == data.Length) { segmentNumber = (((((i - rowLength) / rowLength) + 1) * rowLength).ToString("X4")); buffer.AppendLine(segmentNumber + " " + bytes.PadRight(rowLength * 3 + 1, ‘ ‘) + " " + ascii); } } return buffer.ToString(); }
截图示例:
数据按照十六进制文本输入,可自动调节输出宽度
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。