您的位置:首页 > 其它

数据按照十六进制文本输入,可自动调节输出宽度

2014-09-15 09:22 281 查看
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();
}


截图示例:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐