Skip to content

Commit

Permalink
use HexConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
Milkitic committed Feb 17, 2022
1 parent 1691ed5 commit 2591c13
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
9 changes: 0 additions & 9 deletions src/KbinXml.Net/Readers/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,4 @@ private void Realign16_8()
if (_pos16 % 4 == 0)
_pos16 = _pos32;
}

private static char ToCharLower(int value)
{
value &= 0xF;
value += '0';

if (value > '9') value += ('a' - ('9' + 1));
return (char)value;
}
}
24 changes: 1 addition & 23 deletions src/KbinXml.Net/Writers/DataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void WriteBinary(string value)
if (arr != null) span = span.Slice(0, length);
try
{
FillHexBuilder(ref span, value);
HexConverter.TryDecodeFromUtf16(value.AsSpan(), span);
Write32BitAligned(span);
}
finally
Expand Down Expand Up @@ -152,27 +152,5 @@ private void Pad(int target)
Stream.WriteByte(0);
}
}

private static void FillHexBuilder(ref Span<byte> builder, string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");

for (int i = 0; i < builder.Length; ++i)
{
builder[i] = ((byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1]))));
}
}

private static int GetHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
//return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
}
}

0 comments on commit 2591c13

Please sign in to comment.