Fix printhex helper ignoring width parameter.

This commit is contained in:
2022-10-12 14:18:38 +01:00
parent 248a808e49
commit 118563e086

View File

@@ -57,8 +57,8 @@ public static class PrintHex
// TODO: Color list // TODO: Color list
// TODO: Allow to change width // TODO: Allow to change width
var str = "Offset"; var str = "Offset";
int rows = array.Length / 16; int rows = array.Length / width;
int last = array.Length % 16; int last = array.Length % width;
int offsetLength = $"{array.Length:X}".Length; int offsetLength = $"{array.Length:X}".Length;
var sb = new StringBuilder(); var sb = new StringBuilder();
@@ -66,7 +66,7 @@ public static class PrintHex
rows++; rows++;
if(last == 0) if(last == 0)
last = 16; last = width;
if(offsetLength < str.Length) if(offsetLength < str.Length)
offsetLength = str.Length; offsetLength = str.Length;
@@ -80,7 +80,7 @@ public static class PrintHex
sb.Append(str); sb.Append(str);
sb.Append(" "); sb.Append(" ");
for(var i = 0; i < 16; i++) for(var i = 0; i < width; i++)
sb.AppendFormat(" {0:X2}", i); sb.AppendFormat(" {0:X2}", i);
if(color) if(color)
@@ -103,8 +103,8 @@ public static class PrintHex
sb.Append("\u001b[0m"); sb.Append("\u001b[0m");
sb.Append(" "); sb.Append(" ");
int lastBytes = i == rows - 1 ? last : 16; int lastBytes = i == rows - 1 ? last : width;
int lastSpaces = 16 - lastBytes; int lastSpaces = width - lastBytes;
for(var j = 0; j < lastBytes; j++) for(var j = 0; j < lastBytes; j++)
{ {
@@ -121,7 +121,7 @@ public static class PrintHex
for(var j = 0; j < lastBytes; j++) for(var j = 0; j < lastBytes; j++)
{ {
int v = array[b]; int v = array[b];
sb.Append(v > 31 && v < 127 || v > 159 ? (char)v : '.'); sb.Append(v is > 31 and < 127 or > 159 ? (char)v : '.');
b++; b++;
} }