Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning values to a public byte array in one line C#

I am working with a public byte array and would like to assign values, like I assign arrays inside a method, i.e

byte[] foo = {0x32, 0x00, 0x1E, 0x00};

but when I define the values I am forced to do

foo[0] = 0x32;
foo[1] = 0x00;
foo[2] = 0x1E;
foo[3] = 0x00;

If I use the first example VS gives an error "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"

If it helps any, the array is always 4 bytes.

my code

public byte[] SetSpeed = new byte[4];
private void trackBar1_Scroll(object sender, EventArgs e)
{
    if (trackBar1.Value == 0)
    {
        try
        {
            stop = true;
            UpdateSThread.Abort();
            Thread.Sleep(350);
        }
        catch { }
        SetSpeed = {0x00,0x00,0x00,0x00};
        WriteMem(GetPlayer() + STATUS_OFFSET, SetSpeed);
        label1.Text = "Normal";              
    }
}
like image 468
user2340475 Avatar asked Aug 19 '13 16:08

user2340475


People also ask

How to assign values to an array of 32 bytes?

The integer value is stored in the next 32 bytes. Without using assembly you can only assign values to bytes array byte by byte using the index of the array. Like so, Here, you have the bytes array with length 1 which is assigned a single byte value.

How to store any value in bytes array in C++?

You can use assembly to store any value in bytes array. function test () public pure returns (uint256 returnValue) { bytes memory b = new bytes (32); assembly { mstore (add (b, 32), 5) returnValue := mload (add (b, 32)) } } In the above code we initialize a bytes array with length 32.

How to assign values to the elements of an array?

Thus, the first way of assigning values to the elements of an array is by doing so at the time of its declaration. And the second method is declaring the array first and then assigning values to its elements. You can understand this by treating n [0], n [1] and n [2] as similar to different variables you used before.

How many bits are there in a 32-byte array?

It can store an integer of size 256 bits in the 32 bytes it has. If we look at the content of variable b in memory, it will return 32 and that is the length of the array. The integer value is stored in the next 32 bytes. Without using assembly you can only assign values to bytes array byte by byte using the index of the array. Like so,


2 Answers

Your first example is fine but it may only be used in a declaration. The elements must be implicitly convertible to the element type. The size is determined from the number of elements given.

byte[] foo = { 0x32, 0x00, 0x1E, 0x00 };

Alternatively you can also d this

byte[] foo = new byte[4];
foo[0] = 0x32;
foo[1] = 0x00;
foo[2] = 0x1E;
foo[3] = 0x00;

as stated above that the syntax that you are trying to use can only be used in a declaration. so try like this.

public byte[] SetSpeed;
private void trackBar1_Scroll(object sender, EventArgs e)
{
    if (trackBar1.Value == 0)
    {
        try
        {
            stop = true;
            UpdateSThread.Abort();
            Thread.Sleep(350);
        }
        catch { }

        //note it will always create a new array
        SetSpeed = new byte[]{0x00,0x00,0x00,0x00}; 
        WriteMem(GetPlayer() + STATUS_OFFSET, SetSpeed);
        label1.Text = "Normal";              
    }
}
like image 173
Ehsan Avatar answered Oct 14 '22 23:10

Ehsan


You can only use the form where you don't specify new when you're declaring a variable. So this is fine:

byte[] x = { 1, 2, 3 };

But this isn't:

byte[] x;
x = { 1, 2, 3 };

Instead you need something like:

byte[] x;
x = new byte[] { 1, 2, 3 };

Or for cases where type inference works in your favour and you're using C# 3 or higher, you can use an implicitly typed array:

string[] x;
x = new[] { "a", "b", "c" };

However, you really need to decide whether you want to create a new array, or whether you want to mutate the existing array. Given that you're just setting all the values to 0, you could instead use:

Array.Clear(SetSpeed, 0, 4);

EDIT: Now that the question contains this:

I would like to change values in an existing array,

then using

SetSpeed = new byte[] { ... }

is not appropriate, as that will change the value of SetSpeed to refer to a different array.

To update an existing array, you could either just stick with the 4 separate statements, or perhaps extract it to a separate method with 4 parameters. It's not really clear what the values are going to be, which makes it hard to give you the best possible code.

like image 24
Jon Skeet Avatar answered Oct 14 '22 22:10

Jon Skeet