【问题标题】:c# Buffer explanationc#缓冲区解释
【发布时间】:2016-02-08 09:59:10
【问题描述】:

这可能是一个真正的初学者问题,但我一直在阅读这方面的内容,但我发现它很难理解。

这是来自msdn 页面的关于此主题的示例(只是稍微小一点)。

using System;

class SetByteDemo
{
    // Display the array contents in hexadecimal.
    public static void DisplayArray(Array arr, string name)
    {
        // Get the array element width; format the formatting string.
        int elemWidth = Buffer.ByteLength(arr) / arr.Length;
        string format = String.Format(" {{0:X{0}}}", 2 * elemWidth);

        // Display the array elements from right to left.
        Console.Write("{0,7}:", name);
        for (int loopX = arr.Length - 1; loopX >= 0; loopX--)
            Console.Write(format, arr.GetValue(loopX));
        Console.WriteLine();
    }

    public static void Main()
    {
        // These are the arrays to be modified with SetByte.
        short[] shorts = new short[2];

        Console.WriteLine("Initial values of arrays:\n");

        // Display the initial values of the arrays.
        DisplayArray(shorts, "shorts");

        // Copy two regions of source array to destination array,
        // and two overlapped copies from source to source.
        Console.WriteLine("\n" +
            "  Array values after setting byte 1 = 1 and byte 3 = 200\n");
        Buffer.SetByte(shorts, 1, 1);
        Buffer.SetByte(shorts, 3, 10);

        // Display the arrays again.
        DisplayArray(shorts, "shorts");
        Console.ReadKey();
    }
}

SetByte 应该很容易理解,但是如果我在执行SetByte 操作之前打印短裤数组,则数组看起来像这样

{short[2]}
    [0]: 0
    [1]: 0

做完第一个Buffer.SetByte(shorts, 1, 1);后数组就变成了

{short[2]}
    [0]: 256
    [1]: 0

设置Buffer.SetByte(shorts, 3, 10);后数组变成

{short[2]}
    [0]: 256
    [1]: 2560

最后,在示例中,他们从右到左打印数组:

0A00 0100

我不明白这是如何工作的,谁能给我一些关于这个的信息?

【问题讨论】:

    标签: c# arrays buffer


    【解决方案1】:

    Buffer 类允许您像在 c 中使用 void 指针一样操作内存,它就像 memcpy、memset 等的总和,以快速操作 .net 上的内存。

    当您传递“shorts”数组时,Buffer 类将其“视为”指向四个连续字节的指针(两个 shorts,每个字节两个字节):

      |[0][1]|[2][3]|
       short  short
    

    所以未初始化的数组是这样的:

     |[0][0]|[0][0]|
      short  short
    

    当您执行Buffer.SetByte(shorts, 1, 1); 时,您会指示 Buffer 类更改字节数组上的第二个字节,因此它将是:

    |[0][1]|[0][0]|
     short  short
    

    如果将两个字节 (0x00, 0x01) 转换为短字节,则为 0x0100(注意这是两个字节一个接一个,但顺序相反,这是因为 C# 编译器使用小端序),或 256

    第二行基本一样Buffer.SetByte(shorts, 3, 10);将第三个字节改为10:

    |[0][1]|[0][10]|
     short  short
    

    然后 0x00,0x0A 作为短片就是 0x0A00 或 2560。

    【讨论】:

      【解决方案2】:

      .NET 类型使用little endianness。这意味着shortint 等的第一个字节(实际上是第 0 个字节)包含最低有效位。

      设置数组后看起来像byte[]:

      0, 1, 0, 10
      

      short[] 是这样解释的:

      0 + 1*256 = 256, 0 + 10*256 = 2560
      

      【讨论】:

        【解决方案3】:

        我认为人们可能会遇到的问题是Buffer.SetByte() 方法基本上与数组索引器 [] 的常规赋值不同,它会根据包含类型的宽度分隔数组(短裤/双打/等)而不是字节......使用你的例子: 短数组通常被视为 arr = [xxxx, yyyy](以 16 为基数) 但是 SetByte 方法将其“视为”: arr = [xx, yy, zz, ww]

        所以像Buffer.SetByte(arr, 1, 5) 这样的调用将寻址数组中的第二个字节,该字节仍在第一个短字节内。在那里设置值,就是这样。 结果应如下所示:

        [05 00, 00 00] 十六进制或 [1280,0]。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-06
          • 2017-10-08
          • 2021-11-13
          • 2013-01-09
          • 2017-09-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多