Byte[] to ASCII

后端 未结 5 662
孤独总比滥情好
孤独总比滥情好 2021-02-04 23:30

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);
5条回答
  •  悲&欢浪女
    2021-02-05 00:03

    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.

    using (var reader = new StreamReader(stream, Encoding.ASCII))
    {
        string theString = reader.ReadToEnd();
        // do something with theString
    }
    

提交回复
热议问题