Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I created a byte array with two strings. How do I convert a byte array to strings?

var binWriter = new BinaryWriter(new MemoryStream());
binWriter.Write("value1");
binWriter.Write("value2");
binWriter.Seek(0, SeekOrigin.Begin);

byte[] result = reader.ReadBytes((int)binWriter.BaseStream.Length);

I want to convert result to strings. I can do it using BinaryReader. But I can not use BinaryReader (it does not supported).

share|improve this question

marked as duplicate by slugster, p.s.w.g, RGraham, Sindre Sorhus, Suma Aug 12 '13 at 7:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
You already have the strings, so you can't actually be doing exactly this - what exactly are you doing? –  harold Jul 25 '12 at 16:45
    
@harold from a «value1/2» strings I guess it's just an example. –  Hi-Angel Mar 30 at 11:59

5 Answers 5

up vote 312 down vote accepted

Depending on the encoding you wish to use:

var str = System.Text.Encoding.Default.GetString(result);
share|improve this answer
8  
You should also use the encoding class to write the string to a byte array. –  Servy Jul 25 '12 at 16:43
3  
That actually gives a funny result, because he wrote the string with the BinaryWriter.Write(string) overload, which first saves the length of the string. –  harold Jul 25 '12 at 16:44
    
Note that this will sanitize the bytes that go into the string to conform to the encoding. If you want to test a bad sequence of bytes for that encoding, use the BlockCopy method below by HforHisham. –  Nick Westgate Sep 2 '14 at 5:34
string convert = "This is the string to be converted";

// From string to byte array
byte[] buffer = Encoding.UTF8.GetBytes(convert);

// From byte array to string
string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
share|improve this answer
1  
You're using String->Byte via ASCII and Byte->String via UTF8. You should use the same encoding for both directions. –  Sujay Sarma Dec 4 '14 at 15:42
    
@SujaySarma Yes, I actually noticed that before, but didn't get around to fixing it. Thanks! –  Andy0708 Dec 4 '14 at 16:39
    
Happily reversed. Thanks! :) –  Sujay Sarma Dec 5 '14 at 8:36

You can do it without dealing with encoding by using BlockCopy:

char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
string str = new string(chars);
share|improve this answer
    
This didn't work for me. I got an error on the length (not dividing gives the length I needed). After getting the correct length I got an argumentException trying to construct the string. –  David Silva Smith Oct 20 '13 at 16:20
    
This only works if your string was encoded with UTF16 which is c# string's default internal encoding scheme. If, say, the byte array was encoded simply with ASCII chars from the original string (assuming it can be), after the BlockCopy, each char will be squeezed with two such ASCII characters, which is clearly wrong. –  KFL Aug 14 '14 at 6:41
1  
On the flipside, if you're trying to create a damaged string for testing, this is exactly the way to do it. Thanks! –  Nick Westgate Sep 2 '14 at 5:28
    
I don't think you need to worry about encoding if you only want to convert to string and then back, see stackoverflow.com/questions/472906/… –  mcmillab Sep 15 '14 at 6:00

To convert the byte[] to string[], simply use the below line.

byte[] fileData; // Some byte array
//Convert byte[] to string[]
var table = (Encoding.Default.GetString(
                 fileData, 
                 0, 
                 fileData.Length - 1)).Split(new string[] { "\r\n", "\r", "\n" },
                                             StringSplitOptions.None);
share|improve this answer
9  
This answer has already been provided. Please upvote instead of reposting. –  ChargingPun May 22 '13 at 12:51

An alternative option is:

string convert = "This is the string to be converted";
convert.CopyTo(0, buffer, 0, convert.length);

See String.CopyTo (MSDN).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.