3

I´m trying to convert a byte array to a delimited by comma string. I just want the values of de bytes into a string so I cand send a string to another pc via TCP.

This is the code that i´m running now and it´s working but it´s too slow (the byte array has 50000 elements). Do you have any better idea?.

Thanks.

byte[] bytes = (byte[])dt.Rows[0]["LNL_BLOB"];
string foto="";

foreach (byte b in bytes)
{
   foto = foto + "," + b.ToString();
}
2
  • 1
    That seems like a terrible way to accomplish whatever it is you are doing. If you're placing a comma between each literal byte, then you're doubling the size of every single communication between your clients. Can you explain why it is you are doing this? Perhaps we can help you find a better solution to your issue. Commented Mar 3, 2011 at 14:54
  • I just have to send a string with some data to a service running. The string is something like this NAME|LASTNAME|SSNO|PHOTO where PHOTO is the byte array converted to string Commented Mar 3, 2011 at 15:24

5 Answers 5

7

Use StringBuilder when doing lots of string operations. In this special case, you can also use string.Join:

string foto = string.Join(",", bytes);
Sign up to request clarification or add additional context in comments.

Comments

6

Well, you're allocating 100000 strings (half from the ToString() calls, half for the intermediate strings). Have you never heard about the dangers of string concatenation, and the purpose of StringBuilder?

E.g.

byte[] bytes = (byte[])dt.Rows[0]["LNL_BLOB"];
System.Text.StringBuilder foto=new System.Text.StringBuilder();

foreach (byte b in bytes)
{
   foto.AppendFormat(",{0}",b);
}
return foto.ToString(); /* Or however you're using your string now */

Comments

4

You could use Convert.ToBase64String rather than iterating through the bytes yourself.

byte[] data = // whatever you do to get the bytes
string sData = Convert.ToBase64String(data);

Here is the method documentation.

When you wanted to get your byte array back from the string, simply use the Convert.FromBase64String ala

byte[] imageData = Convert.FromBase64String(sData);

1 Comment

Just to note: this won't give the same output as the OP's code. This is good, though, since the string will be significantly smaller than a comma-separated list of decimal values, and it will work for "sending a string to another pc via TCP".
1

Use a StringBuilder it is more efficient for concat on strings.

byte[] bytes = (byte[])dt.Rows[0]["LNL_BLOB"];
StringBuilder foto = new StringBuilder();

for(int i = 0; i < bytes.Length; i++) {
  foto.Append(bytes[i].ToString());

  if (i != (bytes.Length - 1)) foto.Append(",");
}

Comments

0

You can parallelize the loop and process different areas of the array in parallel and reassemble the results. And, as others have mentioned, use StringBuilder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.