Parsing a string into a Vector of bytes most efficiently
Given a string with an even amount of characters, what's the most efficient way to parse that into a
Vector<byte>
along with another Vector<byte>
of bit masks?
A sample input is 12 34 ?? 78 87 ?5 ?3 21
-- these are meant to be hex bytes -- which would get turned into < 0x12 0x34 0x00 0x78 0x87 0x05 0x03 0x21 >
for the values, and < 0xFF 0xFF 0x00 0xFF 0xFF 0x0F 0x0F 0xFF >
for the masks.
Currently I'm doing the input.Length % 2 != 0
check first, of course, followed by Regex.Matches(input, @"..").Select(match => match.Value).ToList();
.
From here I do this to parse all of the bytes;
Obviously the big one is using Regex. Any ideas?6 Replies
what type is the bytes variable?
string[]
. Using the example, the contents are [ "12", "34", "??", "78", "87", "?5", "?3", "21" ]
watch me optimizing removing whitespace from a stringwhy use question marks instead of 0. Is that just for the mask?
for the mask, yeah
it should be any non-hexdigit character
š
you can change the logic inside the for loop to this
if you want to be funky you can doInstead
char.IsAsciiHexDigit
if anything