Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Last active May 13, 2018 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakesays-old/014e08bf643d6413f32c487487a5a1f4 to your computer and use it in GitHub Desktop.
Save jakesays-old/014e08bf643d6413f32c487487a5a1f4 to your computer and use it in GitHub Desktop.
void Main()
{
// var y = new string('A', 536_870_912);
var y = "AB4C98";
var x = ParseHexString(y);
x.Dump();
}
byte[] ParseHexString(string hex)
{
var destLength = hex.Length / 2;
var source = hex.ToCharArray();
var dest = new byte[destLength];
int srcIndex = 0;
int Nibble(int c)
{
if (c >= 'A')
{
return (c - 'A') + 10;
}
return c - '0';
}
for (var dstIndex = 0; dstIndex < destLength; dstIndex++)
{
var hn = src[srcIndex++];
if (hn == '\r' || hn == '\n')
{
srcIndex++;
continue;
}
var b = Nibble(hn) << 4 | Nibble(source[srcIndex++]);
dest[dstIndex] = (byte)b;
}
return dest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment