Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ByteExtensions with ToHexWithPrefix and Extend ByteHelpe… #315

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/CodeDoc/Atc/Atc.Helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ ByteHelper.
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`value`&nbsp;&nbsp;-&nbsp;&nbsp;The value.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`checkValue`&nbsp;&nbsp;-&nbsp;&nbsp;The check value.<br />
#### ToStringWithPrefix
>```csharp
>string ToStringWithPrefix(byte[] bytes)
>```
><b>Summary:</b> Converts a byte array to its hexadecimal string representation with a '0x' prefix for each byte and separated with ', '. <code>{ 0x1A, 0x2B, 0x3C }.ToStringWithPrefix() // Gives: "0x1A, 0x2B, 0x3C"</code>
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`bytes`&nbsp;&nbsp;-&nbsp;&nbsp;The byte array to be converted.<br />
>
><b>Returns:</b> A string representation of the byte array in hexadecimal format, prefixed with '0x' for each byte and separated with ', '.
>
><b>Code example:</b>
>```csharp
>byte[] exampleBytes = { 0x1A, 0x2B, 0x3C };
>string hex = ByteHelper.ToStringWithPrefix(exampleBytes);
>Console.WriteLine(hex); // Outputs: 0x1A, 0x2B, 0x3C
>```

<br />

Expand Down
3 changes: 3 additions & 0 deletions docs/CodeDoc/Atc/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -4424,6 +4424,7 @@
- CreateZeroArray(int size)
- HasBit(byte value, byte checkValue)
- HasBit(byte value, int checkValue)
- ToStringWithPrefix(byte[] bytes)
- [CardinalDirectionTypeHelper](Atc.Helpers.md#cardinaldirectiontypehelper)
- Static Methods
- GetByRotationNumberClockwiseUsingMedium(int rotationNumber)
Expand Down Expand Up @@ -4973,6 +4974,7 @@
- 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)
- ToHexWithPrefix(this byte[] value)
- [ByteSizeExtensions](System.md#bytesizeextensions)
- Static Methods
- Bytes(this decimal value)
Expand Down Expand Up @@ -5095,6 +5097,7 @@
- ToUpDownType(this SortDirectionType sortDirectionType)
- [EnumExtensions](System.md#enumextensions)
- Static Methods
- AreFlagsSet(this Enum enumeration, Enum flags)
- GetDescription(this Enum enumeration, bool useLocalizedIfPossible = True)
- GetName(this Enum enumeration)
- IsSet(this Enum enumeration, Enum matchTo)
Expand Down
57 changes: 55 additions & 2 deletions docs/CodeDoc/Atc/System.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,50 @@ Extensions for the byte class.
>```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>Summary:</b> Converts a byte array to its hexadecimal string representation. Examples: <code>{ 0x1A, 0x2B, 0x3C }.ToHex() // Gives: "1A2B3C"</code><code>{ 0x1A, 0x2B, 0x3C }.ToHex("-") // Gives: "1A-2B-3C"</code><code>{ 0x1A, 0x2B, 0x3C }.ToHex("-", true) // Gives: "0x1A-0x2B-0x3C"</code><code>{ 0x1A, 0x2B, 0x3C }.ToHex(", ", true) // Gives: "0x1A, 0x2B, 0x3C"</code>
>
><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.
>
><b>Code example:</b>
>```csharp
> Here are several examples of using the ToHex method:
>
> byte[] exampleBytes = { 0x1A, 0x2B, 0x3C };
>
> // Example without separator
> Console.WriteLine(exampleBytes.ToHex()); // Outputs: 1A2B3C
>
> // Example with separator
> Console.WriteLine(exampleBytes.ToHex("-")); // Outputs: 1A-2B-3C
>
> // Example with separator and hex sign
> Console.WriteLine(exampleBytes.ToHex("-", true)); // Outputs: 0x1A-0x2B-0x3C
>
> // Example with separator and hex sign - Note: Same as exampleBytes.ToHexWithPrefix()
> Console.WriteLine(exampleBytes.ToHex(", ", true)); // Outputs: 0x1A, 0x2B, 0x3C
>```
#### ToHexWithPrefix
>```csharp
>string ToHexWithPrefix(this byte[] value)
>```
><b>Summary:</b> Converts a byte array to its hexadecimal string representation with a '0x' prefix for each byte and separated with ', '. Examples: <code>{ 0x1A, 0x2B, 0x3C }.ToHexWithPrefix() // Gives: "0x1A, 0x2B, 0x3C"</code>
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`value`&nbsp;&nbsp;-&nbsp;&nbsp;The byte array to be converted.<br />
>
><b>Returns:</b> A string representation of the byte array in hexadecimal format, prefixed with '0x' for each byte and separated with ', '.
>
><b>Code example:</b>
>```csharp
>byte[] exampleBytes = { 0x1A, 0x2B, 0x3C };
>string hex = ToHexWithPrefix(exampleBytes);
>Console.WriteLine(hex); // Outputs: 0x1A, 0x2B, 0x3C
>```

<br />

Expand Down Expand Up @@ -1407,6 +1443,23 @@ Extension methods for enumerations.

### Static Methods

#### AreFlagsSet
>```csharp
>bool AreFlagsSet(this Enum enumeration, Enum flags)
>```
><b>Summary:</b> Determines whether all specified flags of another enumeration are set in the current enumeration.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`enumeration`&nbsp;&nbsp;-&nbsp;&nbsp;The enumeration to check for flags.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`flags`&nbsp;&nbsp;-&nbsp;&nbsp;The flags to verify within the enumeration.<br />
>
><b>Returns:</b> True if all specified flags are set; otherwise, false.
>
><b>Code example:</b>
>```csharp
>bool areFlagsSet = DayOfWeek.Monday.AreFlagsSet(DayOfWeek.Monday);
>Assert.True(areFlagsSet);
>```
#### GetDescription
>```csharp
>string GetDescription(this Enum enumeration, bool useLocalizedIfPossible = True)
Expand Down Expand Up @@ -1452,7 +1505,7 @@ Extension methods for enumerations.
>```csharp
>bool IsSet(this Enum enumeration, Enum matchTo)
>```
><b>Summary:</b> Determines whether the specified enumeration match to another enumeration.
><b>Summary:</b> Determines whether the specified enumeration match another enumeration.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`enumeration`&nbsp;&nbsp;-&nbsp;&nbsp;The enumeration.<br />
Expand Down
45 changes: 45 additions & 0 deletions src/Atc/Extensions/BaseTypes/ByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,36 @@ IEnumerable<byte[]> SplitSource()

/// <summary>
/// Converts a byte array to its hexadecimal string representation.
/// Examples:
/// <code>{ 0x1A, 0x2B, 0x3C }.ToHex() // Gives: "1A2B3C"</code>
/// <code>{ 0x1A, 0x2B, 0x3C }.ToHex("-") // Gives: "1A-2B-3C"</code>
/// <code>{ 0x1A, 0x2B, 0x3C }.ToHex("-", true) // Gives: "0x1A-0x2B-0x3C"</code>
/// <code>{ 0x1A, 0x2B, 0x3C }.ToHex(", ", true) // Gives: "0x1A, 0x2B, 0x3C"</code>
/// </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>
/// <example>
/// Here are several examples of using the ToHex method:
/// <code>
/// byte[] exampleBytes = { 0x1A, 0x2B, 0x3C };
///
/// // Example without separator
/// Console.WriteLine(exampleBytes.ToHex()); // Outputs: 1A2B3C
///
/// // Example with separator
/// Console.WriteLine(exampleBytes.ToHex("-")); // Outputs: 1A-2B-3C
///
/// // Example with separator and hex sign
/// Console.WriteLine(exampleBytes.ToHex("-", true)); // Outputs: 0x1A-0x2B-0x3C
///
/// // Example with separator and hex sign - Note: Same as exampleBytes.ToHexWithPrefix()
/// Console.WriteLine(exampleBytes.ToHex(", ", true)); // Outputs: 0x1A, 0x2B, 0x3C
/// </code>
/// </example>
public static string ToHex(
this byte[] value,
string? separator = null,
Expand All @@ -195,4 +218,26 @@ public static string ToHex(
? "0x" + s.Replace("-", $"{separator}0x", StringComparison.Ordinal)
: s.Replace("-", separator, StringComparison.Ordinal);
}

/// <summary>
/// Converts a byte array to its hexadecimal string representation with a '0x' prefix for each byte
/// and separated with ', '.
/// Examples:
/// <code>{ 0x1A, 0x2B, 0x3C }.ToHexWithPrefix() // Gives: "0x1A, 0x2B, 0x3C"</code>
/// </summary>
/// <param name="value">The byte array to be converted.</param>
/// <returns>
/// A string representation of the byte array in hexadecimal format, prefixed with '0x' for each byte
/// and separated with ', '.
/// </returns>
/// <example>
/// <code>
/// byte[] exampleBytes = { 0x1A, 0x2B, 0x3C };
/// string hex = ToHexWithPrefix(exampleBytes);
/// Console.WriteLine(hex); // Outputs: 0x1A, 0x2B, 0x3C
/// </code>
/// </example>
public static string ToHexWithPrefix(
this byte[] value)
=> ToHex(value, separator: ", ", showHexSign: true);
}
40 changes: 33 additions & 7 deletions src/Atc/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@ namespace System;
/// </summary>
public static class EnumExtensions
{
/// <summary>Determines whether the specified enumeration match to another enumeration.</summary>
/// <summary>
/// Determines whether all specified flags of another enumeration are set in the current enumeration.
/// </summary>
/// <param name="enumeration">The enumeration to check for flags.</param>
/// <param name="flags">The flags to verify within the enumeration.</param>
/// <returns>True if all specified flags are set; otherwise, false.</returns>
/// <example>
/// <code>
/// bool areFlagsSet = DayOfWeek.Monday.AreFlagsSet(DayOfWeek.Monday);
/// Assert.True(areFlagsSet);
/// </code>
/// </example>
public static bool AreFlagsSet(
this Enum enumeration,
Enum flags)
=> IsSet(enumeration, flags);

/// <summary>Determines whether the specified enumeration match another enumeration.</summary>
/// <param name="enumeration">The enumeration.</param>
/// <param name="matchTo">The enumeration to match.</param>
/// <returns>true on match; otherwise false.</returns>
/// <code><![CDATA[bool match = DayOfWeek.Monday.IsSet(DayOfWeek.Monday);]]></code>
/// <example><![CDATA[
/// Assert.True(DayOfWeek.Monday.IsSet(DayOfWeek.Monday));
/// ]]></example>
public static bool IsSet(this Enum enumeration, Enum matchTo)
public static bool IsSet(
this Enum enumeration,
Enum matchTo)
{
if (enumeration is null)
{
Expand All @@ -36,7 +55,8 @@ public static bool IsSet(this Enum enumeration, Enum matchTo)
/// <example><![CDATA[
/// Assert.Equal("Monday", DayOfWeek.Monday.GetName());
/// ]]></example>
public static string GetName(this Enum enumeration)
public static string GetName(
this Enum enumeration)
{
if (enumeration is null)
{
Expand Down Expand Up @@ -65,7 +85,9 @@ public static string GetName(this Enum enumeration)
/// <example><![CDATA[
/// Assert.Equal("Monday", DayOfWeek.Monday.GetDescription());
/// ]]></example>
public static string GetDescription(this Enum enumeration, bool useLocalizedIfPossible = true)
public static string GetDescription(
this Enum enumeration,
bool useLocalizedIfPossible = true)
{
if (enumeration is null)
{
Expand Down Expand Up @@ -107,7 +129,8 @@ public static string GetDescription(this Enum enumeration, bool useLocalizedIfPo
/// Converts the named constant to <see langword="string"/> in upper case.
/// </summary>
/// <param name="enumeration">The enum.</param>
public static string ToStringUpperCase(this Enum enumeration)
public static string ToStringUpperCase(
this Enum enumeration)
{
if (enumeration is null)
{
Expand All @@ -122,7 +145,8 @@ public static string ToStringUpperCase(this Enum enumeration)
/// </summary>
/// <param name="enumeration">The enum.</param>
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Triggers warnings, but is irrelevant for this case.")]
public static string ToStringLowerCase(this Enum enumeration)
public static string ToStringLowerCase(
this Enum enumeration)
{
if (enumeration is null)
{
Expand All @@ -138,7 +162,9 @@ public static string ToStringLowerCase(this Enum enumeration)
/// <param name="enumeration">The enumeration.</param>
/// <param name="expression">The expression.</param>
/// <returns>The string attribute value of the enumeration.</returns>
private static TExpected GetAttributeValue<T, TExpected>(this Enum enumeration, Func<T, TExpected> expression)
private static TExpected GetAttributeValue<T, TExpected>(
this Enum enumeration,
Func<T, TExpected> expression)
where T : Attribute
{
if (enumeration is null)
Expand Down
21 changes: 21 additions & 0 deletions src/Atc/Helpers/ByteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,25 @@ public static bool HasBit(
byte value,
int checkValue)
=> (value & checkValue) > 0;

/// <summary>
/// Converts a byte array to its hexadecimal string representation with a '0x' prefix for each byte
/// and separated with ', '.
/// <code>{ 0x1A, 0x2B, 0x3C }.ToStringWithPrefix() // Gives: "0x1A, 0x2B, 0x3C"</code>
/// </summary>
/// <param name="bytes">The byte array to be converted.</param>
/// <returns>
/// A string representation of the byte array in hexadecimal format, prefixed with '0x' for each byte
/// and separated with ', '.
/// </returns>
/// <example>
/// <code>
/// byte[] exampleBytes = { 0x1A, 0x2B, 0x3C };
/// string hex = ByteHelper.ToStringWithPrefix(exampleBytes);
/// Console.WriteLine(hex); // Outputs: 0x1A, 0x2B, 0x3C
/// </code>
/// </example>
public static string ToStringWithPrefix(
byte[] bytes)
=> bytes.ToHexWithPrefix();
}
1 change: 0 additions & 1 deletion test/Atc.Tests/CodeComplianceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class CodeComplianceTests
typeof(LoggerExtensions),
typeof(FileHelper),
typeof(FileHelper<>),
typeof(ByteHelper),
typeof(ByteExtensions),
typeof(StackTraceHelper),

Expand Down
Loading