-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished support for sending messages by typing in Hex values in the Gui
- Loading branch information
1 parent
0397e71
commit aeedc1c
Showing
8 changed files
with
231 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,6 @@ public enum SendFormattingMode | |
|
||
Escaped, | ||
|
||
Hex | ||
BinaryHex | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using MessageCommunicator.Util; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace MessageCommunicator.Tests | ||
{ | ||
[TestClass] | ||
public class HexFormatUtilTests | ||
{ | ||
[TestMethod] | ||
public void BytesToHexString() | ||
{ | ||
var testArray = new byte[] {1, 2, 3, 16, 17, 255}; | ||
|
||
var hexString = HexFormatUtil.ToHexString(testArray); | ||
|
||
Assert.IsTrue(hexString == "01 02 03 10 11 FF"); | ||
} | ||
|
||
[TestMethod] | ||
public void HexStringToBytes() | ||
{ | ||
var hexString = "01 02 03 10 11 FF"; | ||
|
||
var byteArray = HexFormatUtil.ToByteArray(hexString); | ||
|
||
Assert.IsTrue(byteArray.Length == 6); | ||
Assert.IsTrue(byteArray[0] == 1); | ||
Assert.IsTrue(byteArray[1] == 2); | ||
Assert.IsTrue(byteArray[2] == 3); | ||
Assert.IsTrue(byteArray[3] == 16); | ||
Assert.IsTrue(byteArray[4] == 17); | ||
Assert.IsTrue(byteArray[5] == 255); | ||
} | ||
|
||
[TestMethod] | ||
public void HexStringToBytes_WithoutSpaces() | ||
{ | ||
var hexString = "0102031011FF"; | ||
|
||
var byteArray = HexFormatUtil.ToByteArray(hexString); | ||
|
||
Assert.IsTrue(byteArray.Length == 6); | ||
Assert.IsTrue(byteArray[0] == 1); | ||
Assert.IsTrue(byteArray[1] == 2); | ||
Assert.IsTrue(byteArray[2] == 3); | ||
Assert.IsTrue(byteArray[3] == 16); | ||
Assert.IsTrue(byteArray[4] == 17); | ||
Assert.IsTrue(byteArray[5] == 255); | ||
} | ||
|
||
[TestMethod] | ||
public void HexStringToBytes_MixedFormat() | ||
{ | ||
var hexString = " 010203 1 011fF "; | ||
|
||
var byteArray = HexFormatUtil.ToByteArray(hexString); | ||
|
||
Assert.IsTrue(byteArray.Length == 6); | ||
Assert.IsTrue(byteArray[0] == 1); | ||
Assert.IsTrue(byteArray[1] == 2); | ||
Assert.IsTrue(byteArray[2] == 3); | ||
Assert.IsTrue(byteArray[3] == 16); | ||
Assert.IsTrue(byteArray[4] == 17); | ||
Assert.IsTrue(byteArray[5] == 255); | ||
} | ||
|
||
[TestMethod] | ||
public void HexStringToBytes_EmptyString() | ||
{ | ||
var hexString = string.Empty; | ||
|
||
var byteArray = HexFormatUtil.ToByteArray(hexString); | ||
|
||
Assert.IsTrue(byteArray.Length == 0); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ArgumentNullException))] | ||
public void HexStringToBytes_NullString() | ||
{ | ||
HexFormatUtil.ToByteArray(null!); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ArgumentException))] | ||
public void HexStringToBytes_InvalidCharCount() | ||
{ | ||
HexFormatUtil.ToByteArray("01 0"); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ArgumentException))] | ||
public void HexStringToBytes_InvalidCharacter() | ||
{ | ||
HexFormatUtil.ToByteArray("t"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Light.GuardClauses; | ||
|
||
namespace MessageCommunicator.Util | ||
{ | ||
public static class HexFormatUtil | ||
{ | ||
private const string HEX_ALPHABET = "0123456789ABCDEF"; | ||
|
||
public static string ToHexString(byte[] bytes) | ||
{ | ||
return ToHexString(new ArraySegment<byte>(bytes, 0, bytes.Length)); | ||
} | ||
|
||
public static string ToHexString(ArraySegment<byte> bytes) | ||
{ | ||
bytes.MustNotBeDefault(nameof(bytes)); | ||
|
||
if (bytes.Count == 0) { return string.Empty; } | ||
|
||
var length = bytes.Count; | ||
if (length > 1) { length += (length - 1); } | ||
var stringBuffer = StringBuffer.Acquire(length); | ||
var bytesSpan = bytes.AsSpan(); | ||
try | ||
{ | ||
for (var loop = 0; loop < bytesSpan.Length; loop++) | ||
{ | ||
if(stringBuffer.Count > 0){ stringBuffer.Append(' '); } | ||
|
||
var actByte = bytesSpan[loop]; | ||
stringBuffer.Append(HEX_ALPHABET[actByte >> 4]); | ||
stringBuffer.Append(HEX_ALPHABET[actByte & 0xF]); | ||
} | ||
|
||
return stringBuffer.ToString(); | ||
} | ||
finally | ||
{ | ||
StringBuffer.Release(stringBuffer); | ||
} | ||
} | ||
|
||
public static unsafe byte[] ToByteArray(string hexString) | ||
{ | ||
hexString.MustNotBeNull(nameof(hexString)); | ||
|
||
// Count hex characters | ||
var hexCharCount = 0; | ||
for (var loop = 0; loop < hexString.Length; loop++) | ||
{ | ||
if(hexString[loop] == ' '){ continue; } | ||
hexCharCount++; | ||
} | ||
if (hexCharCount == 0) { return new byte[0]; } | ||
if (hexCharCount % 2 != 0) | ||
{ | ||
throw new ArgumentException("Provided uneven count of hex characters!", nameof(hexString)); | ||
} | ||
|
||
// Parse all bytes | ||
var result = new byte[hexCharCount / 2]; | ||
var resultPos = 0; | ||
var hexPos = 0; | ||
var hexValues = stackalloc int[2]; | ||
for (var loop = 0; loop < hexString.Length; loop++) | ||
{ | ||
if(hexString[loop] == ' '){ continue; } | ||
|
||
var asciiValue = (int) (hexString[loop]); | ||
if ((asciiValue > 47) && (asciiValue < 58)) | ||
{ | ||
// 0, 1, 2... | ||
hexValues[hexPos] = asciiValue - 48; | ||
} | ||
else if ((asciiValue > 64) && (asciiValue < 71)) | ||
{ | ||
// A, B, C... | ||
hexValues[hexPos] = (asciiValue - 65) + 10; | ||
} | ||
else if ((asciiValue > 96) && (asciiValue < 103)) | ||
{ | ||
// a, b, c... | ||
hexValues[hexPos] = (asciiValue - 97) + 10; | ||
} | ||
else | ||
{ | ||
throw new ArgumentException($"Invalid hex sign '{hexString[loop]}' at position {loop}!", nameof(hexString)); | ||
} | ||
|
||
hexPos++; | ||
if (hexPos == 2) | ||
{ | ||
result[resultPos] = (byte)(hexValues[0] * 16 + hexValues[1]); | ||
resultPos++; | ||
hexPos = 0; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters