Skip to content

Commit

Permalink
Move ParseHexStringAsByteArray to a class
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Feb 9, 2024
1 parent 924fafd commit 12b147b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static KVValue ParseValue(string text)
if (text.Length == HexStringLengthForUnsignedLong && text.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
var hexadecimalString = text[2..];
var data = ParseHexStringAsByteArray(hexadecimalString);
var data = HexStringHelper.ParseHexStringAsByteArray(hexadecimalString);

if (BitConverter.IsLittleEndian)
{
Expand Down Expand Up @@ -293,20 +293,6 @@ static KVValue ParseValue(string text)
return new KVObjectValue<string>(text, KVValueType.String);
}

static byte[] ParseHexStringAsByteArray(string hexadecimalRepresentation)
{
Require.NotNull(hexadecimalRepresentation, nameof(hexadecimalRepresentation));

var data = new byte[hexadecimalRepresentation.Length / 2];
for (var i = 0; i < data.Length; i++)
{
var currentByteText = hexadecimalRepresentation.Substring(i * 2, 2);
data[i] = byte.Parse(currentByteText, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}

return data;
}

// The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional
// ‘+’ or ‘-’ sign. If base is zero or 16, the string may then include a “0x” prefix, and the number will be read in base 16;
// otherwise, a zero base is taken as 10 (decimal) unless the next character is ‘0’, in which case it is taken as 8 (octal).
Expand Down
21 changes: 21 additions & 0 deletions ValveKeyValue/ValveKeyValue/HexStringHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Globalization;

namespace ValveKeyValue
{
internal static class HexStringHelper
{
public static byte[] ParseHexStringAsByteArray(string hexadecimalRepresentation)
{
Require.NotNull(hexadecimalRepresentation, nameof(hexadecimalRepresentation));

var data = new byte[hexadecimalRepresentation.Length / 2];
for (var i = 0; i < data.Length; i++)
{
var currentByteText = hexadecimalRepresentation.Substring(i * 2, 2);
data[i] = byte.Parse(currentByteText, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}

return data;
}
}
}

0 comments on commit 12b147b

Please sign in to comment.