2012-10-30 19 views
5

我是C++開發人員,最近開始使用WPF。那麼我在我的應用程序中使用Array.Copy(),看起來像我不能完全得到所需的結果。未能在我的WPF應用程序中使用Array.Copy()

我曾在我的C做++應用程序如下:

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 

我做同樣的動作在我的WPF(C#)應用程序如下:

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; 

但它引發我一個錯誤的Array.Copy(sendBuf + memloc, version, 8);作爲Operator '+' cannot be applied to operands of type 'byte[]' and 'int'

怎樣才能做到這一點? :)請幫忙:)

+1

沒錯。 C#沒有瘋狂的隱式數組指針轉換來混淆每個人。 – chris

+0

@chris:那我們怎麼才能達到它的好友? :)任何替代方法? –

+1

我不會是要問的人。我對C#還不是很有經驗,但我喜歡它處理一些我認爲是C/C++缺陷的方式。 – chris

回答

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

感謝您的答覆。這似乎並不奏效。 'sendBuf'和'version'都是空的。 –

+0

哦,你是否試圖將版本複製到sendBuf?如果是這樣,這兩個應該顛倒(以及整數)。另外,它可能需要陣列屬於同一類型。我不確定它是否會嘗試並只投出值 – Brandon

+1

Typemismatch異常被拋出。由於版本是char並且sendbuf是字節。 –

0

有兩種方法可以試試。第一種方式,你可以做使用System.Buffer.BlockCopy()直副本

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

另一種方式是隻得到從所有使用System.Text炭[]字節。編碼命名空間。

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

爲您翻譯。解釋是內聯的。

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; 
+0

@llianPinzon:在Array.Copy之後拋出一個異常,因爲不能將源數組類型分配給目標數組類型。 ' –

+0

適合我。檢查你如何聲明你的'版本'數組。它應該是byte []。與sendBuf相同。看看我如何宣佈它。 –

+0

@llian Pinzon:它工作正常:)但我的下一步是對字符串數組執行相同的操作。 'VAR myDBoards =新[] { 「」, 「S1010013」, 「S1010014」, 「S1010015」, 「S1010017」, };'和需要與此進行的類似的操作。 'memcpy(sendBuf + memloc,myDBoards [temp],8);'where temp = 2即「S1010014」我該如何實現它? :) –

相關問題