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*.ToString();*

}
return b;
}

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.

Look at the answer by patridge. For some reason you are inserting ā€˜,’ which will require you to modify the algorithms listed. For your scenario you are converting sentances or longer byte.

Lookup by byte (via CodesInChaos)
Text: 10,853.96 (45.8X faster)
Sentence: 0.65 (42.7X faster)

I’d recommend this algorithm.