Reputation: 666
This is really getting the better of me. I need to convert this:
string data = "4,0,0,0,1,0,0,0,16,0,0,0,100,58,82,80,162,77,200,183,178,32"
into a byte array so that I can use it here:
polKey.SetValue("Blob", data, RegistryValueKind.Binary);
I've tried data.Split(',')
to split it into an array and use that, but I can't get my head around it.
Upvotes: 2
Views: 5801
Reputation: 12546
You can use Linq
string data = "4,0,0,0,1,0,0,0,16,0,0,0,100,58,82,80,162,77,200,183,178,32";
var buf = data.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => byte.Parse(x))
.ToArray();
A regex solution is also possible
var buf = Regex.Matches(data,@"\d+").Cast<Match>()
.Select(x => byte.Parse(x.Value))
.ToArray();
Upvotes: 1
Reputation: 152626
I'm assuming your byte array needs the parsed values (e.g. 4, 0, 1, 100, etc.) and not the ASCII values of each string.
First convert to an array of strings:
string[] strings = data.Split(',');
then convert each string to a byte:
byte[] bytes = strings.Select(s => byte.Parse(s)).ToArray();
Upvotes: 5