- Home /
Fastest C# Byte[] to String conversion?
Hi, I want to convert byte[] to String to be saved into iCloud Key-Value storage. I have successfully convert the data, but the process is too slow. The data is a ghosting data from several levels which has elements up to 40k. The size of the file is not big, around 100kb. But It could take 2 minutes to finish.
Here is the code I used :
Byte[] to String :
string ByteArrayToString(byte[] val)
{
string b = "";
int len = val.Length;
for(int i = 0; i < len; i++) {
if(i != 0) {
b += ",";
}
b += val[i].ToString();
}
return b;
}
Comment
Answer by Dave-Carlile · Jun 24, 2015 at 12:58 PM
Does the string need to be comma delimited, or just an encoded version of the byte data? I would give Convert.ToBase64String a try. It will have the added benefit of being a smaller string.
You can convert back with Convert.FromBase64String.