4

Ok, before anyone attempts to label this as a duplicate, I am not asking for a string to a byte array. I want a string array, containing something similar like this: {"5","168","188","28","29","155"} to be converted to a byte array. I have searched, and was only able to find string to byte array, which is quite different. Thanks.

Edit: the array will be preset so that each member is parsable through byte.Parse, so this is not an issue.

Ari
  • 3,489
  • 5
  • 26
  • 47
  • 1
    Please provide a more specific example. What do you mean? – SLaks Jun 29 '12 at 20:28
  • See this answer on serialization: http://stackoverflow.com/q/1446547/228080 – Ryan Bennett Jun 29 '12 at 20:28
  • 1
    Do you have a string array, like `new string[] {"5","168","188"}` or a string containing an array like syntax, like `"[5,168,188]"`? – Guffa Jun 29 '12 at 20:29
  • Without more context - specifically of what you are trying to achieve with this transformation, we can't really help. – Oded Jun 29 '12 at 20:30
  • Are you trying to take the string literal "[5,168,188,28,29,155]" and convert it to the equivalent ` new byte[]{5,168,188,28,29,155}`? – James Jun 29 '12 at 20:30
  • Any requirements around the format of the output? BinaryFormatter from System.Runtime.Serialization.Formatters could get the job done. Alternatively, you could convert each string to a byte array (ensure that it is null terminated) and combine those arrays, etc. Please provide some more detail so we know how to better help you. – mlorbetske Jun 29 '12 at 20:30
  • Do you mean foreach-ing over the string array and calling Byte.Parse() on each iteration? – benjer3 Jun 29 '12 at 20:30
  • @Guffa The first one is what I meant, sorry for not clarifying. – Ari Jun 29 '12 at 20:31
  • 4
    You can still edit your question and clarify it. – Oded Jun 29 '12 at 20:32
  • @RyanBennett This worked for me! Thanks for saving my day. – Harold_Finch Jun 20 '20 at 22:58

4 Answers4

12

This will fail for anything that can't be parsed by byte.Parse

var str = new[] {"5", "168", "188", "28", "29", "155"};
var byt = str.Select(byte.Parse).ToArray();
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
6

You have to parse each string into a byte and put in a new array. You can just loop though the items and convert each one:

string[] strings = { "5", "168", "188", "28", "29", "155" };
byte[] bytes = new byte[strings.length];
for (int i = 0; i < strings.Length; i++) {
  bytes[i] = Byte.Parse(strings[i]);
}

You can also use the Array.ConvertAll method for this:

string[] strings = { "5", "168", "188", "28", "29", "155" };
byte[] bytes = Array.ConvertAll(strings, Byte.Parse);

You can also use LINQ extensions to do it:

string[] strings = { "5", "168", "188", "28", "29", "155" };
bytes = strings.Select(Byte.Parse).ToArray();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Assuming you mean that you have a string array like string[] myArray = {"5","168",...};:

myArray.Select(x => byte.Parse(x)).ToArray();

Or:

myArray.Select(byte.Parse).ToArray();
Tim S.
  • 55,448
  • 7
  • 96
  • 122
2

Try this

With linq

string[] strings = new[] { "1", "2", "3", "4" };
byte[] byteArray = strings.Select(x =>  byte.Parse(x)).ToArray();

Without linq

string[] strings = { "1", "2", "3", "4" };

byte[] bytArr = new byte[strings.Length];

int i = 0;

foreach(String text in strings)
{
  bytArr[i++] = byte.Parse(text);  
}
Adil
  • 146,340
  • 25
  • 209
  • 204