c# - Is data “lost” when using binary data in a string?

后端 未结 9 1852
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 13:58

I\'ve tried reading a JPG file using the StreamReader class\' ReadToEnd() method which returns a string.

For some reason though, when I wri

9条回答
  •  Happy的楠姐
    2021-01-16 14:43

    Strings are for text data. They're not for binary data - if you use them this way you will lose data (there are encodings you can use which won't lose data if you're lucky, but there are subtle issues which still make it a really bad idea.)

    If you're actually dealing with a file, the easiest way of reading the whole thing is to call File.ReadAllBytes. If you have to deal with an arbitrary stream, have a look at "Creating a byte array from a stream".

提交回复
热议问题