Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Buffer explanation

Tags:

arrays

c#

buffer

This might be a really beginer's question but I've been reading about this and I'm finding it hard to understand.

This is a sample from the msdn page about this subject (just a little smaller).

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 should be easy to understand, but if I print the shorts array before doing the SetByte operation the array looks like this

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

After doing the first Buffer.SetByte(shorts, 1, 1); the array becomes

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

And after setting Buffer.SetByte(shorts, 3, 10); the array becomes

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

At the end, in the example they print the array from right to left:

0A00 0100

I don't understand how this works, can someone give me to some information about this?

like image 573
miguelmpn Avatar asked Jul 01 '26 22:07

miguelmpn


2 Answers

The Buffer class allows you to manipulate memory as if you were using a void pointer in c, it's like a sum of memcpy, memset, and so on to manipulate in a fast way memory on .net .

When you passed the "shorts" array, the Buffer class "sees" it as a pointer to four consecutive bytes (two shorts, each of them two bytes) :

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

So the uninitialized array looks like this:

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

When you do Buffer.SetByte(shorts, 1, 1); you instruct the Buffer class to change the second byte on the byte array, so it will be:

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

If you convert the two bytes (0x00, 0x01) to a short it is 0x0100 (note as these are the two bytes one after other, but in reverse order, that's because the C# compiler uses little endianness), or 256

The second line basically does the same Buffer.SetByte(shorts, 3, 10);changes third byte to 10:

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

And then 0x00,0x0A as a short is 0x0A00 or 2560.

like image 165
Gusman Avatar answered Jul 04 '26 00:07

Gusman


The .NET types use little endianness. That means that the first byte (0th, actually) of a short, int, etc. contains the least significant bits.

After setting the array it seems like this as byte[]:

0, 1, 0, 10

As short[] it is interpreted like this:

0 + 1*256 = 256, 0 + 10*256 = 2560
like image 36
György Kőszeg Avatar answered Jul 04 '26 01:07

György Kőszeg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!