简体   繁体   中英

Unable to pass array in function in c# (Byte [ ])

In function aes_encrypt() I want to pass only first 16 elements of an array not whole array. I am new to C#. I am getting an error that "Cannot Convert from byte to byte []"

byte[][] packet = new byte[12][];
private static byte[] key_dec_pswd = new byte[16];
for (int loop = 0; loop < noofbyte; loop++)
{
   packet[addr / 512][addr % 512 + 4 + loop] = Convert.ToByte(RawDataLine.Substring((9 + (loop * 2)), 2), 16);
}
int j = packet[i].Length;
byte[] tmpPacket = new byte[j];

for (int k = 0; k < j; k++)
{
   tmpPacket[k] = packet[i][k];
}

aes_encrypt(tmpPacket[16], key_dec_pswd); //Getting Error here. Cannot Convert from byte to byte []

public static void aes_encrypt(byte[] chlng_byte, byte[] key)
{
   aes128_encrypt(chlng_byte, key);
}

Change

aes_encrypt(tmpPacket[16], key_dec_pswd);

to

aes_encrypt(tmpPacket, key_dec_pswd);

tmpPacket[16] passes only byte 16 into the function instead of the intended array and thus the error.

You can also simplify the byte array copy from:

int j = packet[i].Length;
byte[] tmpPacket = new byte[j];

for (int k = 0; k < j; k++)
{
   tmpPacket[k] = packet[i][k];
}

to:

var tmpPacket = packet[i].ToArray();

This does the allocation and copy all in one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM