HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How to convert byte array to string

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
arrayhowbyteconvertstring

Problem

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

Solution

Depending on the encoding you wish to use:

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

Code Snippets

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

Context

Stack Overflow Q#11654562, score: 1457

Revisions (0)

No revisions yet.