Steven Wilson
Steven Wilson

Reputation: 139

Failing to use Array.Copy() in my WPF App

I am a C++ developer and recently started working on WPF. Well I am using Array.Copy() in my app and looks like I am not able to completely get the desired result.

I had done in my C++ app as follows:

static const signed char version[40] = {
'A', 'U', 'D', 'I', 'E', 'N', 'C', 'E',   // name
 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,   // reserved,  firmware size
 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,   // board number
 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,   // variant, version, serial
 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0     // date code, reserved
};

unsigned char sendBuf[256] = {};
int memloc = 0;
sendBuf[memloc++] = 0;
sendBuf[memloc++] = 0;

// fill in the audience header
memcpy(sendBuf+memloc, version, 8); // the first 8 bytes
memloc += 16; // the 8 copied, plus 8 reserved bytes

I did the similar operation in my WPF (C#) app as follows:

Byte[] sendBuf = new Byte[256];

char[] version = 
        {
                'A', 'U', 'D', 'I', 'E', 'N', 'C', 'E',         // name
                '0', '0', '0', '0', '0', '0', '0', '0' ,        // reserved,  firmware size
                '0', '0', '0', '0', '0', '0', '0', '0' ,        // board number
                '0', '0', '0', '0', '0', '0', '0', '0' ,        // variant, version, serial
                '0', '0', '0', '0', '0', '0', '0', '0'          // date code, reserved
        };

// fill in the address to write to -- 0
        sendBuf[memloc++] = 0;
        sendBuf[memloc++] = 0;

        // fill in the audience header
        Array.Copy(sendBuf + memloc, version, 8);   // the first 8 bytes
        memloc += 16;

But it throws me an error at Array.Copy(sendBuf + memloc, version, 8); as Operator '+' cannot be applied to operands of type 'byte[]' and 'int'.

How can achieve this???? :) please help :)

Upvotes: 5

Views: 687

Answers (3)

Ilian
Ilian

Reputation: 5355

Translated it for you. Explanations are inline.

var sendBuf = new byte[256];

// char in C# is a UTF-16 character (at least 2 bytes). Use byte[] here instead.
byte[] version = 
        {
                (byte)'A', (byte)'U', (byte)'D', (byte)'I', (byte)'E', (byte)'N', (byte)'C', (byte)'E',         // name
                0, 0, 0, 0, 0, 0, 0, 0,        // reserved,  firmware size
                0, 0, 0, 0, 0, 0, 0, 0,        // board number
                0, 0, 0, 0, 0, 0, 0, 0,        // variant, version, serial
                0, 0, 0, 0, 0, 0, 0, 0         // date code, reserved
        };

int memloc = 0;

sendBuf[memloc++] = 0;
sendBuf[memloc++] = 0;

// C# doesn't allow pointer arithmetic with arrays. memcpy's signature is also reversed.
Array.Copy(
   version,     // Source
   0,           // Index of Source
   sendBuf,     // Destination
   memloc,      // Index of Destination
   8);          // The first 8 bytes
memloc += 16;

Upvotes: 1

nickm
nickm

Reputation: 1775

Two ways you can try. First way you can do a straight copy using System.Buffer.BlockCopy()

e.g

// fill in the audience header
Buffer.BlockCopy(version, memloc, sendBuf, 0, 8);   // the first 8 bytes

The other way is to just get all the bytes from the char[] using the System.Text.Encoding namespace.

sendBuf = System.Text.Encoding.ASCII.GetBytes(version);

Upvotes: 0

Brandon
Brandon

Reputation: 238

for(int i = 0; i < 8; i++)
{
    sendBuf[i+memloc] = Convert.ToSByte(version[i]);
}

Upvotes: 2

Related Questions