mirror of
https://github.com/aaru-dps/Aaru.Helpers.git
synced 2025-12-16 19:24:35 +00:00
Move to file scoped namespaces.
This commit is contained in:
155
PrintHex.cs
155
PrintHex.cs
@@ -33,104 +33,103 @@
|
||||
using System.Text;
|
||||
using Aaru.Console;
|
||||
|
||||
namespace Aaru.Helpers
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Helper operations to get hexadecimal representations of byte arrays</summary>
|
||||
public static class PrintHex
|
||||
{
|
||||
/// <summary>Helper operations to get hexadecimal representations of byte arrays</summary>
|
||||
public static class PrintHex
|
||||
/// <summary>Prints a byte array as hexadecimal values to the console</summary>
|
||||
/// <param name="array">Array</param>
|
||||
/// <param name="width">Width of line</param>
|
||||
public static void PrintHexArray(byte[] array, int width = 16) =>
|
||||
AaruConsole.WriteLine(ByteArrayToHexArrayString(array, width));
|
||||
|
||||
/// <summary>Prints a byte array as hexadecimal values to a string</summary>
|
||||
/// <param name="array">Array</param>
|
||||
/// <param name="width">Width of line</param>
|
||||
/// <param name="color">Use ANSI escape colors for sections</param>
|
||||
/// <returns>String containing hexadecimal values</returns>
|
||||
public static string ByteArrayToHexArrayString(byte[] array, int width = 16, bool color = false)
|
||||
{
|
||||
/// <summary>Prints a byte array as hexadecimal values to the console</summary>
|
||||
/// <param name="array">Array</param>
|
||||
/// <param name="width">Width of line</param>
|
||||
public static void PrintHexArray(byte[] array, int width = 16) =>
|
||||
AaruConsole.WriteLine(ByteArrayToHexArrayString(array, width));
|
||||
if(array is null)
|
||||
return null;
|
||||
|
||||
/// <summary>Prints a byte array as hexadecimal values to a string</summary>
|
||||
/// <param name="array">Array</param>
|
||||
/// <param name="width">Width of line</param>
|
||||
/// <param name="color">Use ANSI escape colors for sections</param>
|
||||
/// <returns>String containing hexadecimal values</returns>
|
||||
public static string ByteArrayToHexArrayString(byte[] array, int width = 16, bool color = false)
|
||||
// TODO: Color list
|
||||
// TODO: Allow to change width
|
||||
string str = "Offset";
|
||||
int rows = array.Length / 16;
|
||||
int last = array.Length % 16;
|
||||
int offsetLength = $"{array.Length:X}".Length;
|
||||
var sb = new StringBuilder();
|
||||
|
||||
if(last > 0)
|
||||
rows++;
|
||||
|
||||
if(last == 0)
|
||||
last = 16;
|
||||
|
||||
if(offsetLength < str.Length)
|
||||
offsetLength = str.Length;
|
||||
|
||||
while(str.Length < offsetLength)
|
||||
str += ' ';
|
||||
|
||||
if(color)
|
||||
sb.Append("\u001b[36m");
|
||||
|
||||
sb.Append(str);
|
||||
sb.Append(" ");
|
||||
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
if(array is null)
|
||||
return null;
|
||||
sb.AppendFormat(" {0:X2}", i);
|
||||
}
|
||||
|
||||
// TODO: Color list
|
||||
// TODO: Allow to change width
|
||||
string str = "Offset";
|
||||
int rows = array.Length / 16;
|
||||
int last = array.Length % 16;
|
||||
int offsetLength = $"{array.Length:X}".Length;
|
||||
var sb = new StringBuilder();
|
||||
if(color)
|
||||
sb.Append("\u001b[0m");
|
||||
|
||||
if(last > 0)
|
||||
rows++;
|
||||
sb.AppendLine();
|
||||
|
||||
if(last == 0)
|
||||
last = 16;
|
||||
int b = 0;
|
||||
|
||||
if(offsetLength < str.Length)
|
||||
offsetLength = str.Length;
|
||||
|
||||
while(str.Length < offsetLength)
|
||||
str += ' ';
|
||||
string format = $"{{0:X{offsetLength}}}";
|
||||
|
||||
for(int i = 0; i < rows; i++)
|
||||
{
|
||||
if(color)
|
||||
sb.Append("\u001b[36m");
|
||||
|
||||
sb.Append(str);
|
||||
sb.Append(" ");
|
||||
|
||||
for(int i = 0; i < 16; i++)
|
||||
{
|
||||
sb.AppendFormat(" {0:X2}", i);
|
||||
}
|
||||
sb.AppendFormat(format, b);
|
||||
|
||||
if(color)
|
||||
sb.Append("\u001b[0m");
|
||||
|
||||
sb.AppendLine();
|
||||
sb.Append(" ");
|
||||
int lastBytes = i == rows - 1 ? last : 16;
|
||||
int lastSpaces = 16 - lastBytes;
|
||||
|
||||
int b = 0;
|
||||
|
||||
string format = $"{{0:X{offsetLength}}}";
|
||||
|
||||
for(int i = 0; i < rows; i++)
|
||||
for(int j = 0; j < lastBytes; j++)
|
||||
{
|
||||
if(color)
|
||||
sb.Append("\u001b[36m");
|
||||
|
||||
sb.AppendFormat(format, b);
|
||||
|
||||
if(color)
|
||||
sb.Append("\u001b[0m");
|
||||
|
||||
sb.Append(" ");
|
||||
int lastBytes = i == rows - 1 ? last : 16;
|
||||
int lastSpaces = 16 - lastBytes;
|
||||
|
||||
for(int j = 0; j < lastBytes; j++)
|
||||
{
|
||||
sb.AppendFormat(" {0:X2}", array[b]);
|
||||
b++;
|
||||
}
|
||||
|
||||
for(int j = 0; j < lastSpaces; j++)
|
||||
sb.Append(" ");
|
||||
|
||||
b -= lastBytes;
|
||||
sb.Append(" ");
|
||||
|
||||
for(int j = 0; j < lastBytes; j++)
|
||||
{
|
||||
int v = array[b];
|
||||
sb.Append((v > 31 && v < 127) || v > 159 ? (char)v : '.');
|
||||
b++;
|
||||
}
|
||||
|
||||
sb.AppendLine();
|
||||
sb.AppendFormat(" {0:X2}", array[b]);
|
||||
b++;
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
for(int j = 0; j < lastSpaces; j++)
|
||||
sb.Append(" ");
|
||||
|
||||
b -= lastBytes;
|
||||
sb.Append(" ");
|
||||
|
||||
for(int j = 0; j < lastBytes; j++)
|
||||
{
|
||||
int v = array[b];
|
||||
sb.Append((v > 31 && v < 127) || v > 159 ? (char)v : '.');
|
||||
b++;
|
||||
}
|
||||
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user