742

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

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 a string. I could do it using BinaryReader, but I cannot use BinaryReader (it is not supported).

LHM
  • 557
  • 8
  • 25
Oksana
  • 12,112
  • 8
  • 49
  • 88

4 Answers4

1210

Depending on the encoding you wish to use:

var str = System.Text.Encoding.Default.GetString(result);
eulerfx
  • 34,062
  • 5
  • 57
  • 81
  • 28
    You should also use the encoding class to write the string to a byte array. – Servy Jul 25 '12 at 16:43
  • 7
    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 02 '14 at 05:34
  • 9
    Also it's important to be aware that `System.Text.Encoding.Default` is the *system's current ANSI code page* - the results of this will vary depending on how the operating system is configured. If you know what encoding the string really is you should use that one instead. – Wolfgang Nov 05 '15 at 15:18
  • 3
    This is wrong. From the code in the question, the encoding is UTF8, the default for BinaryWriter. – Tom Blodget Feb 13 '16 at 02:19
  • 1
    See https://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it – Raedwald Nov 23 '17 at 20:37
305

Assuming that you are using UTF-8 encoding:

string convert = "This is the string to be converted";

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

// From byte array to string
string s = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);
ba0708
  • 8,528
  • 11
  • 61
  • 97
  • 18
    This answer saves readers the inevitable google search for the other conversion direction. – Zoomzoom Jul 25 '18 at 13:08
  • 2
    This answer is more accurate, because of encoding specification and another side conversion. – pcdro Sep 20 '18 at 20:08
  • 4
    What merit does `System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);` bring compared to `System.Text.Encoding.UTF8.GetString(buffer);`? – Wouter Vanherck Nov 06 '18 at 14:24
  • 2
    @WouterVanherck Judging by the [reference source](https://referencesource.microsoft.com/#mscorlib/system/text/encoding.cs,1483), not much. – JAD Mar 01 '19 at 11:55
28

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);
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
HforHisham
  • 1,720
  • 20
  • 34
  • 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
  • 1
    This only works if your string was encoded with UTF16 which is [c# string's default internal encoding scheme](http://stackoverflow.com/questions/14942092/why-does-net-uses-the-utf16-encoding-for-string-but-uses-utf8-as-default-for). 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 06:41
  • 10
    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 02 '14 at 05:28
  • I don't think you need to worry about encoding if you only want to convert to string and then back, see http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array – mcmillab Sep 15 '14 at 06:00
  • 2
    I think if you use `Math.Ceiling` on `bytes.Length / sizeof(char)` then it will always work correctly. – Lukasz Jun 09 '15 at 11:48
11

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);
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mansoor Ali
  • 143
  • 1
  • 2