我有如下代码:
byte[] myBytes = new byte[10 * 10000];
for (long i = 0; i < 10000; i++)
{
byte[] a1 = BitConverter.GetBytes(i);
byte[] a2 = BitConverter.GetBytes(true);
byte[] a3 = BitConverter.GetBytes(false);
byte[] rv = new byte[10];
System.Buffer.BlockCopy(a1, 0, rv, 0, a1.Length);
System.Buffer.BlockCopy(a2, 0, rv, a1.Length, a2.Length);
System.Buffer.BlockCopy(a3, 0, rv, a1.Length + a2.Length, a3.Length);
}
一切运行正常。我试图将此代码转换为使所有内容都写入“myBytes
”,但后来意识到,我使用了一个长整数,如果其值超过了 int.MaxValue
,则转换将失败,应该如何解决?
另一个问题是,由于我不想在内存中创建非常大的字节数组,那么如何直接发送到我的.WriteBytes(path,myBytes);
函数中呢?