diff --git a/docs/CodeDoc/Atc/IndexExtended.md b/docs/CodeDoc/Atc/IndexExtended.md index ca1b340e..50069d26 100644 --- a/docs/CodeDoc/Atc/IndexExtended.md +++ b/docs/CodeDoc/Atc/IndexExtended.md @@ -4895,6 +4895,7 @@ - TakeBytesAndConvertToInt(this byte[] value, int startPosition = 0, int length = 0) - TakeBytesAndConvertToLong(this byte[] value, int startPosition = 0, int length = 0) - TakeRemainingBytes(this byte[] value, int startPosition = 0) + - ToHex(this byte[] value, string separator = null, bool showHexSign = False) - [ByteSizeExtensions](System.md#bytesizeextensions) - Static Methods - Bytes(this decimal value) diff --git a/docs/CodeDoc/Atc/System.md b/docs/CodeDoc/Atc/System.md index 15da8694..547e9ac3 100644 --- a/docs/CodeDoc/Atc/System.md +++ b/docs/CodeDoc/Atc/System.md @@ -286,6 +286,18 @@ Extensions for the byte class. >Parameters:
>     `value`  -  The value.
>     `startPosition`  -  The start position.
+#### ToHex +>```csharp +>string ToHex(this byte[] value, string separator = null, bool showHexSign = False) +>``` +>Summary: Converts a byte array to its hexadecimal string representation. +> +>Parameters:
+>     `value`  -  The byte array to be converted.
+>     `separator`  -  An optional character used to separate the hexadecimal values. If not provided, there will be no separator between values.
+>     `showHexSign`  -  A flag indicating whether to prepend each hexadecimal value with '0x'. Defaults to false.
+> +>Returns: A string representation of the byte array in hexadecimal format.
diff --git a/src/Atc/Extensions/BaseTypes/ByteExtensions.cs b/src/Atc/Extensions/BaseTypes/ByteExtensions.cs index cc1c3caf..58af030f 100644 --- a/src/Atc/Extensions/BaseTypes/ByteExtensions.cs +++ b/src/Atc/Extensions/BaseTypes/ByteExtensions.cs @@ -165,4 +165,34 @@ IEnumerable SplitSource() } } } + + /// + /// Converts a byte array to its hexadecimal string representation. + /// + /// The byte array to be converted. + /// An optional character used to separate the hexadecimal values. If not provided, there will be no separator between values. + /// A flag indicating whether to prepend each hexadecimal value with '0x'. Defaults to false. + /// + /// A string representation of the byte array in hexadecimal format. + /// + public static string ToHex( + this byte[] value, + string? separator = null, + bool showHexSign = false) + { + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } + + var s = BitConverter.ToString(value); + if (separator is null) + { + return s.Replace("-", string.Empty, StringComparison.Ordinal); + } + + return showHexSign + ? "0x" + s.Replace("-", $"{separator}0x", StringComparison.Ordinal) + : s.Replace("-", separator, StringComparison.Ordinal); + } } \ No newline at end of file diff --git a/test/Atc.Tests/Extensions/BaseTypes/ByteExtensionsTests.cs b/test/Atc.Tests/Extensions/BaseTypes/ByteExtensionsTests.cs new file mode 100644 index 00000000..2834e738 --- /dev/null +++ b/test/Atc.Tests/Extensions/BaseTypes/ByteExtensionsTests.cs @@ -0,0 +1,74 @@ +// ReSharper disable AssignNullToNotNullAttribute +// ReSharper disable StringLiteralTypo +namespace Atc.Tests.Extensions.BaseTypes; + +public class ByteExtensionsTests +{ + [Fact] + public void ToHex_NullByteArray() + { + byte[] nullArray = null; + + Assert.Throws(() => nullArray.ToHex()); + } + + [Fact] + public void ToHex_EmptyByteArray() + { + var byteArray = Array.Empty(); + + var actual = byteArray.ToHex(); + + Assert.Equal(string.Empty, actual); + } + + [Fact] + public void ToHex_WithoutSeparator() + { + byte[] byteArray = { 0x12, 0x34, 0x56 }; + + var actual = byteArray.ToHex(); + + Assert.Equal("123456", actual); + } + + [Fact] + public void ToHex_WithSeparator1() + { + byte[] byteArray = { 0x12, 0x34, 0x56 }; + + var actual = byteArray.ToHex("-"); + + Assert.Equal("12-34-56", actual); + } + + [Fact] + public void ToHex_WithSeparator2() + { + byte[] byteArray = { 0x12, 0x34, 0x56 }; + + var actual = byteArray.ToHex(":"); + + Assert.Equal("12:34:56", actual); + } + + [Fact] + public void ToHex_WithSeparatorAndHexSign1() + { + byte[] byteArray = { 0x12, 0x34, 0x56 }; + + var actual = byteArray.ToHex(", ", showHexSign: true); + + Assert.Equal("0x12, 0x34, 0x56", actual); + } + + [Fact] + public void ToHex_WithSeparatorAndHexSign2() + { + byte[] byteArray = { 0x12, 0x34, 0x56 }; + + var actual = byteArray.ToHex(" - ", showHexSign: true); + + Assert.Equal("0x12 - 0x34 - 0x56", actual); + } +} \ No newline at end of file