Byte Array to String in C#

As Im new to C# and I am stuck with a very basic problem.

I am reading one text file, which contains some data(for example "hello") I am reading this data like the code mentioned below.

System.IO.Stream myStream;
Int32 fileLen;
StringBuilder displayString = new StringBuilder();

// Get the length of the file.
fileLen = FileUploadId.PostedFile.ContentLength;

// Display the length of the file in a label.
string strLengthOfFileInByte = "The length of the file is " +
fileLen.ToString() + " bytes.";

// Create a byte array to hold the contents of the file.
Byte[] Input = new Byte[fileLen];
// Initialize the stream to read the uploaded file.
myStream = FileUploadId.FileContent;

// Read the file into the byte array.
//myStream.Read(Input, 0, fileLen);

myStream.Read(Input, 0, fileLen);

// Copy the byte array to a string.
for (int loop1 = 0; loop1 < fileLen; loop1++)
{
    displayString.Append(Input[loop1].ToString());
}

// Display the contents of the file in a 
string strFinalFileContent = displayString.ToString();

return strFinalFileContent;

I want "hello" should be the value of 'strFinalFileContent'. I am getting "104 101 108 108 111" means decimal values of ASCII characters. Please help me how to get "h e l l o" as output. It might me small question, But I m beginner So please help me.

Jon Skeet
people
quotationmark

You should use an Encoding object to specify which encoding you want to use to convert the binary data into text. It's not clear from your post what the input file actually is, or whether you'll know the encoding in advance - but it's much simpler if you do.

I'd advise you to create a StreamReader using the given encoding, wrapping your stream - and read text from that. Otherwise you could get into interesting difficulties reading "half a character" if the character is split across binary reads.

Also note that this line is dangerous:

myStream.Read(Input, 0, fileLen);

You're assuming that this one Read call will read all the data. In general, that's not true for streams. You should always use the return value for Stream.Read (or TextReader.Read) to see how much you've actually read.

In practice, using a StreamReader will make all of this much simpler. Your entire code can be replaced by:

// Replace Encoding.UTF8 with whichever encoding you're interested in. If you
// don't specify an encoding at all, it will default to UTF-8.
using (var reader = new StreamReader(FileUploadId.FileContent, Encoding.UTF8))
{
    return reader.ReadToEnd();
}

people

See more on this question at Stackoverflow