本文介绍了如何将long数据类型转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好,先生,这是我的代码; 当我运行此函数时出现错误无法从long转换为int。 public byte [] readfile( string FPath) { byte [] data = null ; 尝试 { FileInfo FI = new FileInfo(FPath) ; long numbytes = FI.Length; // FileStream fstream = new FileStream(FPath,FileMode.Open,FileAccess.Read); BinaryReader br = new BinaryReader(fstream); // data = br.ReadBytes(Convert.ToInt64(numbytes)); data = br.ReadBytes(Convert.ToInt64(numbytes)); // 上面的行错误就像; // 错误3参数1:无法从'long'转换为'int' fstream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } 返回数据; } 先生告诉我怎么做解决方案 实际上你得到了那个错误,因为BinaryReader.ReadBytes方法接受参数作为整数,并且你要传递很长时间。 所以你可以改变这样的代码。 .. data = br.ReadBytes(Convert.ToInt32(numbytes)); 将解决您的问题 使用WriteByte代替WriteBytes: public byte [] readfile(string FPath) { byte [] data = null; try { FileInfo FI = new FileInfo(FPath); long numbytes = FI.Length; // FileStream fstream = new FileStream(FPath,FileMode.Open,FileAccess.Read); BinaryReader br = new BinaryReader(fstream); MemoryStream ms = new MemoryStream(); for(long i = 0; i< numbytes; i ++) { ms.WriteByte(br.ReadByte()); } ms.Close(); fstream.Close(); data = ms.ToArray(); } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } 返回数据; } hello sir here is my code;when i run this function there is error "cannot convert from long to int."public byte[] readfile(string FPath) { byte[] data = null; try { FileInfo FI = new FileInfo(FPath); long numbytes = FI.Length; // FileStream fstream = new FileStream(FPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fstream); //data = br.ReadBytes(Convert.ToInt64(numbytes)); data = br.ReadBytes(Convert.ToInt64(numbytes));//At above line error comes like;//Error3Argument 1: cannot convert from 'long' to 'int' fstream.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } return data; }sir tell me how to do this 解决方案 Actually you got that error because BinaryReader.ReadBytes method is accepting argument as integer, and you are passing long into it.so you can change code like this...data = br.ReadBytes(Convert.ToInt32(numbytes));will resolve your issueUse "WriteByte" instead of "WriteBytes":public byte[] readfile(string FPath){ byte[] data = null; try { FileInfo FI = new FileInfo(FPath); long numbytes = FI.Length; // FileStream fstream = new FileStream(FPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fstream); MemoryStream ms = new MemoryStream(); for (long i = 0; i < numbytes; i++) { ms.WriteByte(br.ReadByte()); } ms.Close(); fstream.Close(); data = ms.ToArray(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } return data;} 这篇关于如何将long数据类型转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 05:56