Skip to content

Commit

Permalink
Introduced new "StringUtils.ParseHexString" method
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Feb 14, 2022
1 parent a4f048b commit 3fa44a3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Skybrud.Essentials/Strings/StringUtils.Bytes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Skybrud.Essentials.Strings {

Expand Down Expand Up @@ -26,6 +27,25 @@ public static string ToHexString(byte[] bytes, HexFormat format) {
_ => throw new Exception($"Unsupported format: {format}")
};
}

/// <summary>
/// Parse the specified HEX string into a corresponding array of <see cref="byte"/>.
/// </summary>
/// <param name="hex">The HEX string to parse.</param>
/// <returns></returns>
public static byte[] ParseHexString(string hex) {

if (string.IsNullOrWhiteSpace(hex)) throw new ArgumentNullException(nameof(hex));

// Detect whether the HEX string uses hyphens
int mod = hex.Length > 2 && hex[2] == '-' ? 3 : 2;

return Enumerable.Range(0, hex.Length)
.Where(x => x % mod == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();

}

}

Expand Down
71 changes: 71 additions & 0 deletions src/UnitTestProject1/Strings/HexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Skybrud.Essentials.Strings;
using System.Text;

namespace UnitTestProject1.Strings {

[TestClass]
public class HexTests {

[TestMethod]
public void ToHexString() {

byte[] bytes1 = Encoding.UTF8.GetBytes("hello world");
byte[] bytes2 = Encoding.UTF8.GetBytes("rød grød med fløde");
byte[] bytes3 = Encoding.GetEncoding(1252).GetBytes("rød grød med fløde");

string result1 = StringUtils.ToHexString(bytes1);
string result2 = StringUtils.ToHexString(bytes2);
string result3 = StringUtils.ToHexString(bytes3);

Assert.AreEqual("68656c6c6f20776f726c64", result1, "#1");
Assert.AreEqual("72c3b864206772c3b864206d656420666cc3b86465", result2, "#2");
Assert.AreEqual("72f864206772f864206d656420666cf86465", result3, "#3");

}

[TestMethod]
public void ParseHexString() {

string hex1 = "68656c6c6f20776f726c64";
string hex2 = "72c3b864206772c3b864206d656420666cc3b86465";
string hex3 = "72f864206772f864206d656420666cf86465";

byte[] result1 = StringUtils.ParseHexString(hex1);
byte[] result2 = StringUtils.ParseHexString(hex2);
byte[] result3 = StringUtils.ParseHexString(hex3);

string str1 = Encoding.UTF8.GetString(result1);
string str2 = Encoding.UTF8.GetString(result2);
string str3 = Encoding.GetEncoding(1252).GetString(result3);

Assert.AreEqual("hello world", str1, "#1");
Assert.AreEqual("rød grød med fløde", str2, "#2");
Assert.AreEqual("rød grød med fløde", str3, "#3");

}

[TestMethod]
public void ParseHexStringWithHypens() {

string hex1 = "68-65-6c-6c-6f-20-77-6f-72-6c-64";
string hex2 = "72-c3-b8-64-20-67-72-c3-b8-64-20-6d-65-64-20-66-6c-c3-b8-64-65";
string hex3 = "72-f8-64-20-67-72-f8-64-20-6d-65-64-20-66-6c-f8-64-65";

byte[] result1 = StringUtils.ParseHexString(hex1);
byte[] result2 = StringUtils.ParseHexString(hex2);
byte[] result3 = StringUtils.ParseHexString(hex3);

string str1 = Encoding.UTF8.GetString(result1);
string str2 = Encoding.UTF8.GetString(result2);
string str3 = Encoding.GetEncoding(1252).GetString(result3);

Assert.AreEqual("hello world", str1, "#1");
Assert.AreEqual("rød grød med fløde", str2, "#2");
Assert.AreEqual("rød grød med fløde", str3, "#3");

}

}

}
1 change: 1 addition & 0 deletions src/UnitTestProject1/UnitTestProject1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Maps\PointTests.cs" />
<Compile Include="Reflection\ReflectionTests.cs" />
<Compile Include="Security\SecurityTests.cs" />
<Compile Include="Strings\HexTests.cs" />
<Compile Include="Strings\HtmlTests.cs" />
<Compile Include="Strings\NameValueCollectionTests.cs" />
<Compile Include="Strings\StringTests.cs" />
Expand Down

0 comments on commit 3fa44a3

Please sign in to comment.