如何在另一个char数组中复制char数组? [英] How can I copy a char array in another char array?

查看:147
本文介绍了如何在另一个char数组中复制char数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



是否有方法或方法在char数组(大小为64)中复制char数组(例如大小为8)。



我尝试了什么:



我已经尝试过使用某些方法像Array.Copy和copyto()但它没有用。

我的结果char数组总是/ 0/0/0/0 .....



 public int CAN_DefineTransmit(int canNo,int iD,int cycleMs,int offsetMs,int length,char [] frame)
{
int ret;
this.ReadBuffer();
this.CANDIAG_CommandBufferInit();

this.buff.Param [1] .IValue = canNo;
this.buff.Param [2] .IValue = iD;
this.buff.Param [3] .IValue = cycleMs;
this.buff.Param [4] .IValue = offsetMs;
this.buff.Param [5] .IValue = length;
if((长度< 8&&长度> = 0)||长度> 8)
{
Array.Copy(frame,0,this.buff.Param [3] .FrameFD,0,长度);
}
else
{
Array.Copy(frame,0,this.buff.Param [3] .FrameFD,0,8);
}

// this.buff.Param [6] .FrameFD = frame;
ret = this.CANDIAG_Client_SendCommand(501);
this.WriteBuffer();
返回this.CANDIAG_CommandBufferExit();
}





帧FD是在结构中声明的64个字符数组。


  char  [] input =   Hello!这是文本。 .ToCharArray(); 
char [] output = new char [ 64 ];
Array.Copy(input,output,input.Length);
for int i = 0 ; i < output.Length; i ++)
{
char c =输出[i];
Console.WriteLine( {0}:{1:X02} char .IsControl(c)?' *' :c,( int )c);
}


此示例应该可以正常工作... Array.copy()将源数组的范围复制到您选择的指定数组。

我希望这会有所帮助:



  char  [] myarray =  new   char  [ 5 ]; 
myarray2 = new char [ 64 ];
复制(myarray,myarray2, 5 );


Hello,

Is there a method or a way to copy a char array (for example of size 8) in a char array (of size 64).

What I have tried:

I already tried using some methods like Array.Copy and copyto() but it didn't worked.
My result char array was always /0 /0 /0 /0 .....

public int CAN_DefineTransmit(int canNo, int iD, int cycleMs, int offsetMs, int length, char[] frame)
{
    int ret;
    this.ReadBuffer();
    this.CANDIAG_CommandBufferInit();

    this.buff.Param[1].IValue = canNo;
    this.buff.Param[2].IValue = iD;
    this.buff.Param[3].IValue = cycleMs;
    this.buff.Param[4].IValue = offsetMs;
    this.buff.Param[5].IValue = length;
    if ((length < 8 && length >= 0) || length > 8)
    {
        Array.Copy(frame, 0, this.buff.Param[3].FrameFD, 0, length);
    }
    else
    {
        Array.Copy(frame, 0, this.buff.Param[3].FrameFD, 0, 8);
    }

    // this.buff.Param[6].FrameFD = frame;
    ret = this.CANDIAG_Client_SendCommand(501);
    this.WriteBuffer();
    return this.CANDIAG_CommandBufferExit();
}



Frame FD is a char array of 64 declared in a structure.

解决方案

Try this:

char[] input = "Hello! This is text.".ToCharArray();
char[] output = new char[64];
Array.Copy(input, output, input.Length);
for(int i = 0; i < output.Length; i++)
    {
    char c = output[i];
    Console.WriteLine("{0}:{1:X02}", char.IsControl(c) ? '*' : c, (int)c);
    }


This example should work fine... Array.copy() copies the range of the source array to the specified array of your choice.
I hope this helps:

char[] myarray = new char[5];
myarray2 = new char[64];
Copy(myarray, myarray2, 5);


这篇关于如何在另一个char数组中复制char数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆