用随机字节填充列表

时间:2022-10-07 20:21:48

I'm trying to fill a buffer with random bytes. The buffer is defined as a list of bytes. This is something that I want to keep as it is. Here is the definition:

我试图用随机字节填充缓冲区。缓冲区被定义为一个字节列表。这是我想保留的。这是定义:

namespace NameofProject
{
    public partial class Form1 : Form
    {
    List<byte> buff = new List<byte>();
    }

}

And my first attempt is

我的第一个尝试是。

public static void RandomFillBuffer()
{
   Random rnd = new Random();
   rnd.NextBytes(buff);
}

Yet this gives such an error for buff: An object reference is required for the non-static field, method, or property 'Form1.buff'

然而这给buff带来了这样的错误:非静态字段、方法或属性“Form1.buff”需要一个对象引用

Then I just deleted the word "static" (I am not sure whether this is true) and it becomes "public void RandomFillBuffer()", but this time I am getting this error for buff: Argument 1: cannot convert from 'System.Collections.Generic.List' to 'byte[]'

然后我删除了单词“static”(我不确定这是不是真的),它变成了“public void RandomFillBuffer()”参数1:不能从' system . collection . generic . generation1转换。列表”到“byte[]”

I'd appreciate any help to solve any of the 2 errors if they make sense.

如果这两个错误中有任何一个是有意义的,我将非常感激你们的帮助。

3 个解决方案

#1


5  

You're getting that problem because NextBytes() expects an array, but you're trying to pass a List<>. One way to solve it would be to change your List<> to an array:

您会遇到这个问题,因为NextBytes()需要一个数组,但是您试图传递一个<>的列表。解决它的一种方法是将列表<>更改为一个数组:

byte[] buff = new byte[someSize];

You're going to have to figure out what someSize should be (it's up to you). You can't fill something without it having a size. Otherwise, how would it know when it's done?

你必须弄清楚什么是身材(由你决定)。没有尺寸就不能填充。否则,它怎么知道什么时候完成呢?

#2


2  

The problem you are having is that NextBytes fills an array[] not a list. You need to define an array with an index of its size

您遇到的问题是NextBytes填充的是一个数组[]而不是一个列表。您需要定义一个具有其大小索引的数组。

    // Put random bytes into this array.
    byte[] array = new byte[8];
    // Fill array with random bytes.
    Random random = new Random();
    random.NextBytes(array);

#3


1  

First: you tried to make your method static (it means that this method is not associated with any instance of the object but instead with the class of objects), and tried to reference not static member from it (your buff is not static and thus is associated with a particular instance, a particular Form in your case). Second: you tried to use Random.NextBytes(System.Byte[]) but provided System.Collections.Generics.List<System.Byte> as an argument.

第一:你试图让你的静态方法(这意味着这种方法不是与任何实例相关联的对象,而是与类对象),并试图从中引用非静态成员(你迷不是静态的,因此是与一个特定的实例相关联,一个特定的形式在你的情况下)。第二:您尝试使用Random.NextBytes(System. byte[]),但是提供了System. collections . generics.list . System。字节>作为参数。

The code below should work for you (this code assumes at least that buff already has some data and so has a positive length):

下面的代码应该为您工作(这段代码假定至少该buff已经有一些数据,所以它的长度是正的):

var generator = new Random();
var array = new Byte[buff.Count]; // create a local array of the same size as your list
generator.NextBytes(array); // fill the array with random bytes
buff = array.ToList(); // copy array to a new list and let field "buff" reference this freshly created list

Please, note, that this code is not optimal because it copies an array. But it does what you want, I guess.

请注意,这段代码不是最优的,因为它复制了一个数组。但我猜它是你想要的。

#1


5  

You're getting that problem because NextBytes() expects an array, but you're trying to pass a List<>. One way to solve it would be to change your List<> to an array:

您会遇到这个问题,因为NextBytes()需要一个数组,但是您试图传递一个<>的列表。解决它的一种方法是将列表<>更改为一个数组:

byte[] buff = new byte[someSize];

You're going to have to figure out what someSize should be (it's up to you). You can't fill something without it having a size. Otherwise, how would it know when it's done?

你必须弄清楚什么是身材(由你决定)。没有尺寸就不能填充。否则,它怎么知道什么时候完成呢?

#2


2  

The problem you are having is that NextBytes fills an array[] not a list. You need to define an array with an index of its size

您遇到的问题是NextBytes填充的是一个数组[]而不是一个列表。您需要定义一个具有其大小索引的数组。

    // Put random bytes into this array.
    byte[] array = new byte[8];
    // Fill array with random bytes.
    Random random = new Random();
    random.NextBytes(array);

#3


1  

First: you tried to make your method static (it means that this method is not associated with any instance of the object but instead with the class of objects), and tried to reference not static member from it (your buff is not static and thus is associated with a particular instance, a particular Form in your case). Second: you tried to use Random.NextBytes(System.Byte[]) but provided System.Collections.Generics.List<System.Byte> as an argument.

第一:你试图让你的静态方法(这意味着这种方法不是与任何实例相关联的对象,而是与类对象),并试图从中引用非静态成员(你迷不是静态的,因此是与一个特定的实例相关联,一个特定的形式在你的情况下)。第二:您尝试使用Random.NextBytes(System. byte[]),但是提供了System. collections . generics.list . System。字节>作为参数。

The code below should work for you (this code assumes at least that buff already has some data and so has a positive length):

下面的代码应该为您工作(这段代码假定至少该buff已经有一些数据,所以它的长度是正的):

var generator = new Random();
var array = new Byte[buff.Count]; // create a local array of the same size as your list
generator.NextBytes(array); // fill the array with random bytes
buff = array.ToList(); // copy array to a new list and let field "buff" reference this freshly created list

Please, note, that this code is not optimal because it copies an array. But it does what you want, I guess.

请注意,这段代码不是最优的,因为它复制了一个数组。但我猜它是你想要的。