mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Adding methods for converting hex strings to byte arrays
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using ICD.Common.Properties;
|
using System.Linq;
|
||||||
|
using ICD.Common.Properties;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Tests
|
namespace ICD.Common.Utils.Tests
|
||||||
@@ -24,6 +25,21 @@ namespace ICD.Common.Utils.Tests
|
|||||||
Assert.AreEqual("\x08\x22\x00\x00\x00\x02", output);
|
Assert.AreEqual("\x08\x22\x00\x00\x00\x02", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase("FF", new byte[] {0xFF})]
|
||||||
|
[TestCase("01FF", new byte[] { 0x01, 0xFF })]
|
||||||
|
public void HexToBytes(string value, byte[] expected)
|
||||||
|
{
|
||||||
|
byte[] bytes = StringUtils.HexToBytes(value);
|
||||||
|
Assert.IsTrue(bytes.SequenceEqual(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("1", 1)]
|
||||||
|
[TestCase("FF", 0xFF)]
|
||||||
|
public void HexToByte(string value, byte expected)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(expected, StringUtils.HexToByte(value));
|
||||||
|
}
|
||||||
|
|
||||||
[TestCase("Test", "Test")]
|
[TestCase("Test", "Test")]
|
||||||
[TestCase("test", "Test")]
|
[TestCase("test", "Test")]
|
||||||
[TestCase("TodayILiveInTheUSAWithSimon", "Today I Live In The USA With Simon")]
|
[TestCase("TodayILiveInTheUSAWithSimon", "Today I Live In The USA With Simon")]
|
||||||
|
|||||||
@@ -175,6 +175,7 @@ namespace ICD.Common.Utils
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
[NotNull]
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static byte[] ToBytes([NotNull] string input)
|
public static byte[] ToBytes([NotNull] string input)
|
||||||
{
|
{
|
||||||
@@ -184,6 +185,43 @@ namespace ICD.Common.Utils
|
|||||||
return Encoding.GetEncoding(28591).GetBytes(input);
|
return Encoding.GetEncoding(28591).GetBytes(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the hex string ("01AB23") to a byte array ({1, 171, 23}).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[NotNull]
|
||||||
|
[PublicAPI]
|
||||||
|
public static byte[] HexToBytes([NotNull] string value)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
throw new ArgumentNullException("value");
|
||||||
|
|
||||||
|
if (value.Length % 2 != 0)
|
||||||
|
throw new ArgumentException("The binary key cannot have an odd number of digits");
|
||||||
|
|
||||||
|
return value.Split(2)
|
||||||
|
.Select(s => HexToByte(s))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts the hex string ("AB") to a byte (171).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public static byte HexToByte([NotNull] string value)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
throw new ArgumentNullException("value");
|
||||||
|
|
||||||
|
if (value.Length == 0 || value.Length > 2)
|
||||||
|
throw new ArgumentOutOfRangeException("value");
|
||||||
|
|
||||||
|
return Convert.ToByte(value, 16);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to parse the string as an integer.
|
/// Attempts to parse the string as an integer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user