ChatGPT解决这个技术问题 Extra ChatGPT

How to convert byte array to string [duplicate]

This question already has answers here: How to convert UTF-8 byte[] to string (16 answers) Closed 8 years ago.

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).

You already have the strings, so you can't actually be doing exactly this - what exactly are you doing?
@harold from a «value1/2» strings I guess it's just an example.
The OP did not accept the answer that assumes UTF-8 encoding, but did accept a different answer, suggesting that it is not safe to assume UTF-8 encoding.
You can just do new String(result)

e
eulerfx

Depending on the encoding you wish to use:

var str = System.Text.Encoding.Default.GetString(result);

You should also use the encoding class to write the string to a byte array.
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.
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.
This is wrong. From the code in the question, the encoding is UTF8, the default for BinaryWriter.
b
ba0708

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);

This answer saves readers the inevitable google search for the other conversion direction.
This answer is more accurate, because of encoding specification and another side conversion.
What merit does System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length); bring compared to System.Text.Encoding.UTF8.GetString(buffer);?
@WouterVanherck Judging by the reference source, not much.
Also work for me BitConverter.ToString(byteArray)) docs.microsoft.com/en-us/dotnet/api/…
P
Peter Mortensen

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);

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.
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.
On the flipside, if you're trying to create a damaged string for testing, this is exactly the way to do it. Thanks!
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/…
I think if you use Math.Ceiling on bytes.Length / sizeof(char) then it will always work correctly.
P
Peter Mortensen

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);