MemoryStream: why convert to byte after readByte

前端 未结 3 695
眼角桃花
眼角桃花 2021-01-17 11:46

In this example from MS, you\'ll notice that after we read a byte from memory stream, it goes into an int which must then be converted to byte. It stikes me as strange that

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 12:19

    When you use ReadByte If the read is successful then the current position within the stream is advanced by one byte. but its designed to return -1 if the end of the stream has been reached.

    Now this would not be a valid value for Byte (its unsigned)

    ms.Read(buf,0,lenth); here lenth is the number of bytes to read from the stream and what you get from ReadByte is first byte its not be used in the this fashion, something like

    byte[] buff = new byte[ms.Length];
    ms.Read(buff , 0, buff .Length);
    

提交回复
热议问题