I received the contents of a text file returned in binary values:
Byte[] buf = new Byte[size];
stream = File.InputStream;
stream.Read(buf, 0, size);
How can I convert this to ASCII?
I received the contents of a text file returned in binary values:
Byte[] buf = new Byte[size];
stream = File.InputStream;
stream.Read(buf, 0, size);
How can I convert this to ASCII?
As an alternative to reading a data from a stream to a byte array, you could let the framework handle everything and just use a
StreamReader
set up with an ASCII encoding to read in the string. That way you don't need to worry about getting the appropriate buffer size or larger data sizes.Encoding.GetString Method (Byte[]) convert bytes to a string.
Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Syntax
Parameters
Return Value
Type: System.String
A String containing the results of decoding the specified sequence of bytes.
Exceptions
Remarks
Use:
System.Text.Encoding.ASCII.GetString(buf);
You can use:
But sometimes you will get a weird number instead of the string you want. In that case, your original string may have some hexadecimal character when you see it. If it's the case, you may want to try this:
Or as a last resort: