Last active
May 13, 2018 05:02
-
-
Save jakesays-old/014e08bf643d6413f32c487487a5a1f4 to your computer and use it in GitHub Desktop.
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
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