I have a SmartCard project that requires saving 16 bytes each time for wider range of SC readers use, not sure about the bytes size to be saved every time restriction, but i have failed using higher numbers of encoding data using the Identive SCL011 reader, and since not all data comes in 16 multiples, of course some looping and assigning after re-sizing the data should take a place.
so the simplest loop i have come up with is as follows
int Index = 0;
for(int i =0; i < blocksNumber; i++)
{
byte temp = new byte[16];
for(int x = 0; x < 16; x++, Index++)
temp[x] = data[Index];
//and encode/write data to the card
}
or
foreach(var item in blocksList)
{
byte temp = new byte[16];
for(int x = 0; x < 16; x++)
temp[x] = data[Index];
//and encode/write data to the card
}
so, in each time i will have to loop through the data to assign it to the Temp array then encode that data, so my questions are:
- it better to for example use
Array.Copy
and - is it faster than looping within the main loop?
- or is there another, faster way?
Edit,,
The updated loop comes with less loops and more readable
int DestinationIndex = 0;
int BlockSize = 16;
int SourceIndex = 0;
try
{
byte[] Data = Encoding.UTF8.GetBytes(txtData.Text.Trim());
Array.Resize(ref Data, 320);
for (int i = 0; i < 20; i++, SourceIndex = SourceIndex + 16)
{
byte[] temp = new byte[16];
Buffer.BlockCopy(Data, SourceIndex, temp, DestinationIndex, BlockSize);
mf.Write(BlocksList[i], temp);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}