Hello.
I am hoping someone can point me in the right direction please.
I am new to C# and writing a simple application that uses a Data Grid to display a range of bytes read in from a file or smart card.
Each row on my Data Grid has an offset address, then 8x byte entries labelled 00, 01, 02 etc.
My custom class takes a byte[] input and loops through, taking 8x bytes at a time and building a <list> of my custom class byte blocks (this is just a class that represents a block of eight bytes and the offset so that I can loop through and add them to the Data Grid.
Now it works fine, providing I have a byte[] structure of a multiple of 8, however, lets say I have byte[18] - now I have 2x byte clocks of 8 bytes, plus 2x bytes left over.
I am trying to figure out how to create the third data block with the left over 2x bytes, and fill the remaining 6 bytes with 0x00, and this is where I am stuck.
I have tried googling but not sure what I am looking for so it is hard to find a solution.
I will try to show the code sections that I feel are relevant, and I would be very grateful if anyone could assist, but please take into consideration I am learning and know my code is likely to be messy :)
<pre>private void BuildDataArray()
{
int dataSize = data.Length;
uint calcOffset = 0x00;
int count = dataSize / 8;
int bytesToTake = 8;
int bytesToSkip = 0;
for (int i = 0; i < count; i++)
{
byte[] tempData = data.Skip(bytesToSkip).Take(bytesToTake).ToArray();
bytesToSkip = bytesToSkip + 8;
bytesToTake = bytesToTake + 8;
ByteBlock tempByteBlock = new ByteBlock(tempData, calcOffset);
calcOffset = calcOffset + 8;
_byteBlock.Add(tempByteBlock);
}
}
This is my ByteBlockArray class that holds an array of byte clocks, which in turn hold 8 byte blocks of data with an offset...
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataTest
{
class ByteBlockArray
{
private List<ByteBlock> _byteBlock = new List<ByteBlock>();
private byte[] data;
private byte offset;
public ByteBlockArray(byte[] data, byte offset)
{
this.data = data;
this.offset = offset;
BuildDataArray();
}
private void BuildDataArray()
{
int dataSize = data.Length;
uint calcOffset = 0x00;
int count = dataSize / 8;
int bytesToTake = 8;
int bytesToSkip = 0;
for (int i = 0; i < count; i++)
{
byte[] tempData = data.Skip(bytesToSkip).Take(bytesToTake).ToArray();
bytesToSkip = bytesToSkip + 8;
bytesToTake = bytesToTake + 8;
ByteBlock tempByteBlock = new ByteBlock(tempData, calcOffset);
calcOffset = calcOffset + 8;
_byteBlock.Add(tempByteBlock);
}
}
public ByteBlock GetByteBlockIndex(int index)
{
return _byteBlock[index];
}
public int GetByteBlockCount()
{
return this._byteBlock.Count;
}
}
}
This is the Byte Block class as described above, and this is the <list> of items that are displayed on my Data Grid...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataTest
{
class ByteBlock
{
private byte _00 = 0x00;
private byte _01 = 0x00;
private byte _02 = 0x00;
private byte _03 = 0x00;
private byte _04 = 0x00;
private byte _05 = 0x00;
private byte _06 = 0x00;
private byte _07 = 0x00;
private byte[] tempData;
private uint calcOffset;
public ByteBlock(byte[] tempData, uint calcOffset)
{
this.tempData = tempData;
this.calcOffset = calcOffset;
FillBytes();
}
public ByteBlock()
{
}
private void FillBytes()
{
this._00 = tempData[0];
this._01 = tempData[1];
this._02 = tempData[2];
this._03 = tempData[3];
this._04 = tempData[4];
this._05 = tempData[5];
this._06 = tempData[6];
this._07 = tempData[7];
}
public uint Offset
{
get { return calcOffset; }
}
public byte i00
{
get { return _00; }
}
public byte i01
{
get { return _01; }
}
public byte i02
{
get { return _02; }
}
public byte i03
{
get { return _03; }
}
public byte i04
{
get { return _04; }
}
public byte i05
{
get { return _05; }
}
public byte i06
{
get { return _06; }
}
public byte i07
{
get { return _07; }
}
}
}
And this is the code I use on the main form to test, passing in a dummy byte array. If the byte array is in multiples of 8 bytes it works fine, if not it wont work as is...
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DataTest
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
List<ByteBlock> dataStructure = new List<ByteBlock>();
public Form1()
{
InitializeComponent();
}
private void ButtonCreateData_Click(object sender, EventArgs e)
{
byte[] data = {0xed, 0xc6, 0x43, 0x32, 0x45, 0xd3, 0x3a, 0x43, 0xed, 0xc6, 0x43, 0x32, 0x45, 0xd3, 0x3a, 0x43};
ByteBlockArray byteArray = new ByteBlockArray(data, 0x00);
int count = byteArray.GetByteBlockCount();
ByteBlock bblock = new ByteBlock();
for (int i = 0; i < count; i++)
{
bblock = byteArray.GetByteBlockIndex(i);
dataStructure.Add(bblock);
}
gridControl1.DataSource = dataStructure;
}
}
}
Also, just to point out, I am using the Data Grid control from DevExpress Winforms subscription.
Again, thank you very much for any help...
What I have tried:
Found out everything so far from my learning books and Google but unable to find out how to get past this point as not even sure what I am searching for.
Thanks