2

I have a array byte[]. It contains the lines of a string[], seperated with line breaks. What would the best way to get the string[] back from the byte[]? Encoding is utf-8.

byte[] lines[];

string[] str = Encoding.UTF8.GetString(lines).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

Would be an option, but maybe there is a better way.

5
  • 1
    The only time I needed that, I used something like this, which looks even worse: using (var ms = new MemoryStream(lines)) { using (var sr = new StreamReader(ms, Encoding.UTF8)) { string line; while ((line = sr.ReadLine()) != null) { // ... } } }. Very curious to find an easier way. Commented Jun 10, 2013 at 10:43
  • 1
    'best' is subjective. Does 'best' mean 'easiest to read the code' or 'most efficient' or 'uses least memory'? The answer will depend on what you want in that regard. Commented Jun 10, 2013 at 10:46
  • @SimonRobinson - Yes, OP didn't specify. I'd be interested in the "most efficient", meaning "fastest" way. Commented Jun 10, 2013 at 10:57
  • @SimonRobinson best for most efficent. Commented Jun 10, 2013 at 12:39
  • 1
    OK in that case I'd stick with the method you've got for now, unless you know the performance is unsatisfactory. If you need something faster, try going through the byte array manually as per my comment in one of the answers. Commented Jun 10, 2013 at 21:51

2 Answers 2

1

I think that solution you provided in quiestion is the best way. I don't know any other methods of getting string from byte array (knowing the encoding) or splitting string into array.

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

1 Comment

Theoretically, one other way would be to iterate through the byte array, looking at each element and using StringBuilders to build the strings as you go. I imagine that would use less memory as you're not using an intermediate string that has the entire text, but the code would be a lot more complex, so I doubt I would ever do that in practice.
0

Dependent on your source of string[], you could potentially use BinaryFormatter and then Serialize your string[] to a byte[] first off and then Deserialize back to a string[] later on; however this means that you will need the string[] first off so it sort of defeats the object...

The code you have mentioned is probably the best way, if I am perfectly honest and as other have suggested - you can read each byte with a StringBuilder but that is probably even messier.

Here is the link to BinaryFormatter

Comments

Your Answer

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