Byte[] to ASCII

后端 未结 5 663
孤独总比滥情好
孤独总比滥情好 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:11

    You can use:

    System.Text.Encoding.ASCII.GetString(buf);
    

    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:

    System.Text.Encoding.UTF8.GetString(buf);
    

    Or as a last resort:

    System.Text.Encoding.Default.GetString(bytearray);
    

提交回复
热议问题