Skip to content

Commit

Permalink
feat: Add byte[] extensions ToHex
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkallesen committed Oct 26, 2023
1 parent c10e696 commit ed17744
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/CodeDoc/Atc/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions docs/CodeDoc/Atc/System.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ Extensions for the byte class.
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`value`&nbsp;&nbsp;-&nbsp;&nbsp;The value.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`startPosition`&nbsp;&nbsp;-&nbsp;&nbsp;The start position.<br />
#### ToHex
>```csharp
>string ToHex(this byte[] value, string separator = null, bool showHexSign = False)
>```
><b>Summary:</b> Converts a byte array to its hexadecimal string representation.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`value`&nbsp;&nbsp;-&nbsp;&nbsp;The byte array to be converted.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`separator`&nbsp;&nbsp;-&nbsp;&nbsp;An optional character used to separate the hexadecimal values. If not provided, there will be no separator between values.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`showHexSign`&nbsp;&nbsp;-&nbsp;&nbsp;A flag indicating whether to prepend each hexadecimal value with '0x'. Defaults to false.<br />
>
><b>Returns:</b> A string representation of the byte array in hexadecimal format.
<br />
Expand Down
30 changes: 30 additions & 0 deletions src/Atc/Extensions/BaseTypes/ByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,34 @@ IEnumerable<byte[]> SplitSource()
}
}
}

/// <summary>
/// Converts a byte array to its hexadecimal string representation.
/// </summary>
/// <param name="value">The byte array to be converted.</param>
/// <param name="separator">An optional character used to separate the hexadecimal values. If not provided, there will be no separator between values.</param>
/// <param name="showHexSign">A flag indicating whether to prepend each hexadecimal value with '0x'. Defaults to false.</param>
/// <returns>
/// A string representation of the byte array in hexadecimal format.
/// </returns>
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);
}
}
74 changes: 74 additions & 0 deletions test/Atc.Tests/Extensions/BaseTypes/ByteExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>(() => nullArray.ToHex());
}

[Fact]
public void ToHex_EmptyByteArray()
{
var byteArray = Array.Empty<byte>();

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);
}
}

0 comments on commit ed17744

Please sign in to comment.