C# 未能在我的WPF应用程序中使用Array.Copy() 我是一个C++开发人员,最近开始工作于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 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;
我在WPF(C#)应用程序中执行了类似的操作,如下所示:C# 未能在我的WPF应用程序中使用Array.Copy() 我是一个C++开发人员,最近开始工作于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 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;,c#,c++,.net,wpf,arrays,C#,C++,.net,Wpf,Arrays,我在WPF(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
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
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)中抛出了一个错误代码>as运算符“+”不能应用于“byte[]”和“int”类型的操作数
如何才能做到这一点??)请帮助:)(inti=0;i<8;i++)
{
sendBuf[i+memloc]=Convert.ToSByte(版本[i]);
}
有两种方法可以尝试。第一种方法是使用System.Buffer.BlockCopy()直接复制
例如
// fill in the audience header
Buffer.BlockCopy(version, memloc, sendBuf, 0, 8); // the first 8 bytes
另一种方法是使用System.Text.Encoding命名空间从char[]获取所有字节
sendBuf = System.Text.Encoding.ASCII.GetBytes(version);
帮你翻译。解释是内联的
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;
这是正确的。C#没有疯狂的隐式数组->指针转换来迷惑所有人。@chris:那我们怎么才能做到呢,伙计?:)还有别的办法吗?我不会问的。我对C还不是很有经验,但我喜欢它处理一些我发现是C/C++缺陷的方式。你能试试Object.MemberwiseClone()吗?你希望sendBuf+memloc做什么?谢谢你的回复。这似乎不起作用。sendBuf
和version
都是空的。哦,您正在尝试将版本复制到sendBuf吗?如果是这样的话,这两个数字(以及整数)应该是相反的。此外,它可能要求阵列的类型相同。我不确定它是否会尝试强制转换这些值,或者引发notTypemismatch异常。因为version是char,sendbuf是byte。您需要使用两个相同的数组类型,或者使用for循环手动复制并强制转换每个值。当然可以。那么,澄清一下,你到底想做什么?将“version”的前8个字节复制到“sendBuf”,从索引“memloc”开始?@llianPinzon:在数组之后引发异常。复制为源数组类型无法分配给目标数组类型。
适合我。检查如何声明“版本”数组。它应该是字节[]。与sendBuf相同。请看一下我是如何声明它的。@llian Pinzon:它工作得很好:),但我的下一步是对字符串数组执行相同的操作<代码>var myDBoards=new[]{“,”S1010013“,”S1010014“,”S1010015“,”S1010017“,}
和类似的操作需要用它来完成<代码>memcpy(sendBuf+memloc,myDBoards[temp],8)代码>其中temp=2,即“S1010014”,我如何实现它?:)不需要重写那么多算法,就可以将数组声明修改为字节数组数组。解决方案应该与上面的类似。@llianPinzon:您的意思是创建字节[]myDBoards和字符串项必须以与version
相同的方式实现。