2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:23 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-07-28 18:13:49 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : PrintHex.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Helpers.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Prints a byte array as hexadecimal.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// ****************************************************************************/
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
using System.Text;
|
2025-08-17 05:50:25 +01:00
|
|
|
using Aaru.Logging;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-11-15 15:58:41 +00:00
|
|
|
namespace Aaru.Helpers;
|
|
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <summary>Helper operations to get hexadecimal representations of byte arrays</summary>
|
|
|
|
|
public static class PrintHex
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2022-03-06 13:29:37 +00:00
|
|
|
/// <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) =>
|
2025-08-17 06:11:22 +01:00
|
|
|
AaruLogging.WriteLine(ByteArrayToHexArrayString(array, width));
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
/// <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)
|
2015-10-05 18:58:13 +01:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(array is null) return null;
|
2019-11-20 00:13:35 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
// TODO: Color list
|
|
|
|
|
// TODO: Allow to change width
|
2022-11-28 10:22:27 +00:00
|
|
|
string str = Localization.Offset;
|
2022-11-15 15:58:41 +00:00
|
|
|
int rows = array.Length / width;
|
|
|
|
|
int last = array.Length % width;
|
|
|
|
|
int offsetLength = $"{array.Length:X}".Length;
|
|
|
|
|
var sb = new StringBuilder();
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2022-11-13 19:38:02 +00:00
|
|
|
switch(last)
|
|
|
|
|
{
|
|
|
|
|
case > 0:
|
|
|
|
|
rows++;
|
2020-01-30 21:51:04 +00:00
|
|
|
|
2022-11-13 19:38:02 +00:00
|
|
|
break;
|
|
|
|
|
case 0:
|
|
|
|
|
last = width;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(offsetLength < str.Length) offsetLength = str.Length;
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
while(str.Length < offsetLength) str += ' ';
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-08-18 22:54:35 +01:00
|
|
|
if(color) sb.Append("\e[36m");
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append(str);
|
|
|
|
|
sb.Append(" ");
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
for(var i = 0; i < width; i++) sb.Append($" {i:X2}");
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-08-18 22:54:35 +01:00
|
|
|
if(color) sb.Append("\e[0m");
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendLine();
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
var b = 0;
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
var format = $"{{0:X{offsetLength}}}";
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
for(var i = 0; i < rows; i++)
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2025-08-18 22:54:35 +01:00
|
|
|
if(color) sb.Append("\e[36m");
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendFormat(format, b);
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-08-18 22:54:35 +01:00
|
|
|
if(color) sb.Append("\e[0m");
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.Append(" ");
|
2022-10-12 14:18:38 +01:00
|
|
|
int lastBytes = i == rows - 1 ? last : width;
|
|
|
|
|
int lastSpaces = width - lastBytes;
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
for(var j = 0; j < lastBytes; j++)
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
2023-10-04 08:16:20 +01:00
|
|
|
sb.Append($" {array[b]:X2}");
|
2022-03-06 13:29:37 +00:00
|
|
|
b++;
|
|
|
|
|
}
|
2019-11-19 01:19:03 +00:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
for(var j = 0; j < lastSpaces; j++) sb.Append(" ");
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
b -= lastBytes;
|
|
|
|
|
sb.Append(" ");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2025-11-24 20:12:10 +00:00
|
|
|
for(var j = 0; j < lastBytes; j++)
|
2022-03-06 13:29:37 +00:00
|
|
|
{
|
|
|
|
|
int v = array[b];
|
2022-10-12 14:18:38 +01:00
|
|
|
sb.Append(v is > 31 and < 127 or > 159 ? (char)v : '.');
|
2022-03-06 13:29:37 +00:00
|
|
|
b++;
|
2019-11-19 01:19:03 +00:00
|
|
|
}
|
2015-10-05 18:58:13 +01:00
|
|
|
|
2022-03-06 13:29:37 +00:00
|
|
|
sb.AppendLine();
|
2015-10-05 18:58:13 +01:00
|
|
|
}
|
2022-03-06 13:29:37 +00:00
|
|
|
|
|
|
|
|
return sb.ToString();
|
2015-10-05 18:58:13 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|