0

I'm converting a List<string> into a byte array like this:

Byte[] bArray = userList
                .SelectMany(s => System.Text.Encoding.ASCII.GetByte(s))
                .ToArray();

How can I convert it back to a List<string>? I tried using ASCII.GetString(s) in the code above, but GetString expected a byte[], not a single byte.

6
  • 1
    You need some way to split your giant byte stream into multiple strings. Commented Jan 30, 2012 at 0:12
  • Something like bArray.ToList()? Or does your original list have multiple strings in it? Commented Jan 30, 2012 at 0:13
  • The original list has multiple strings but I'm not sure how to split it. Alternatively, is there a better way to convert a list of strings to/from a byte array or stream? Commented Jan 30, 2012 at 0:14
  • Why are you doing this again? Commented Jan 30, 2012 at 0:14
  • 4
    @Skoder, is there a better way to convert a list of strings to/from a byte array or stream Yes this is called serialization Commented Jan 30, 2012 at 0:17

4 Answers 4

5

It's not possible to reverse your algorithm.

The problem can be seen if you consider what happens when you have two users called "ab" and "c". This will give the exact same bytes as if you have two users called "a" and "bc". There is no way to distinguish between these two cases with your approach.

Instead of inventing your own serialization format you could just the serialization that is built into the .NET framework, such as the BinaryFormatter.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll use a serializer. Since I had the code above, I thought I may have been missing something simple in order to reverse the process. I'll accept your answer in a few minutes.
3

As a bit of a sidenote, if you preserve the zero-byte string termination you can easily concatenate the strings and extract all information, e.g.

Byte[] bArray = userList
    .SelectMany(s => System.Text.Encoding.ASCII.GetBytes(s + '\0')) // Add 0 byte
    .ToArray();

List<string> names = new List<string>();
for (int i = 0; i < bArray.Length; i++)
{
    int end = i;
    while (bArray[end] != 0) // Scan for zero byte
        end++;
    var length = end - i;
    var word = new byte[length];
    Array.Copy(bArray, i, word, 0, length);
    names.Add(ASCIIEncoding.ASCII.GetString(word));
    i += length;
}

Comments

1

You need to insert a delimter between your strings so that you can split the big byte array back into the original users. The delimiter should be a character which cannot be part of a user name.

Example (assuming | cannot be part of a user name):

var bytes = System.Text.Encoding.ASCII.GetByte(string.Join("|", userList.ToArray()));

Comments

1

You can't do this since the delimiters of the array structure were lost in the SelectMany method.

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.