我需要读取文件中的所有原始字节,然后以字节流的形式发送它们。我的问题是FileStream.ReadByte()返回一个int。我无法将其转换为字节。例如:
FileStream fStream = new FileStream(filePath);
byte readByte = Convert.ToByte(fStream.ReadByte()); // Throws converted value too large for unsigned byte exception!
byteBuffer[index] = readByte; // Need the raw byte for this byte buffer.
如何在C#中解决这个问题?
谢谢,祝周末愉快!
答案 0 :(得分:1)
ReadByte返回byte,强制转换为Int32,如果已到达流末尾则返回-1。当返回-1时,您会在文件末尾获得异常,而不能将其转换为字节。 所以只需检查它,例如
php -m | grep intl