Other good way to convert class instance value to byte List, just raw data?

  • Thread starter Thread starter E-John
  • Start date Start date
E

E-John

Guest
Dear All,

If we want to save the class instance's value to byte list. Just raw data(there will be some tag if Serialization is used. I have used this way before but it is not what I want)

The only way I can figure out is convert each property in class instance manually like below and the result is also correct.

Is there other good way, more programmatical?

Thanks and Best regards,

E-John

namespace TestWinFormObjectToByteList
{
[StructLayout(LayoutKind.Sequential)]
public class TxDataStruct
{
public UInt32 ID;
public ushort Number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] Name = new byte[20];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20 * 32)]
public byte[,] NameBitMap = new byte[20, 32];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] Attribute = new byte[6];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] Company = new byte[8];
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
TxDataStruct tx = new TxDataStruct();

// test object pattern
tx.ID = 0x12345678;
tx.Number = 100;
byte[] strByte = Encoding.ASCII.GetBytes("tx1");
int size = Math.Min(strByte.Length, tx.Name.Length);
for (int i = 0; i < size; i++)
{
tx.Name = strByte;
}

for (int i = 0; i < tx.NameBitMap.GetLength(0); i++)
{
for (int j = 0; j < tx.NameBitMap.GetLength(1); j++)
{
tx.NameBitMap[i, j] = (byte)(i);
}
}
for (int i = 0; i < tx.Attribute.Length; i++)
{
tx.Attribute = (byte)(i + 0x0a);
}
strByte = Encoding.ASCII.GetBytes("VeryWell");
size = Math.Min(strByte.Length, tx.Name.Length);
for (int i = 0; i < size; i++)
{
tx.Company = strByte;
}

// convert class object to byte array
List<byte> listByte = new List<byte>();

listByte.Clear();

// MSB
listByte.Add((byte)((tx.ID >> 24) & 0xff));
listByte.Add((byte)((tx.ID >> 16) & 0xff));
listByte.Add((byte)((tx.ID >> 8) & 0xff));
listByte.Add((byte)((tx.ID >> 0) & 0xff));

listByte.Add((byte)((tx.Number >> 8) & 0xff));
listByte.Add((byte)((tx.Number >> 0) & 0xff));

for (int i = 0; i < tx.Name.Length; i++)
{
listByte.Add(tx.Name);
}

for (int i = 0; i < tx.NameBitMap.GetLength(0); i++)
{
for (int j = 0; j < tx.NameBitMap.GetLength(1); j++)
{
listByte.Add(tx.NameBitMap[i, j]);
}
}

for (int i = 0; i < tx.Attribute.Length; i++)
{
listByte.Add(tx.Attribute);
}

for (int i = 0; i < tx.Company.Length; i++)
{
listByte.Add(tx.Company);
}

ByteViewer byteViewer = new ByteViewer();
// save to file then load file to bv
byteViewer.SetBytes(listByte.ToArray());

// get current directory
string byteListFileName = @"\byteList.bin";
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path += byteListFileName;

// Add create directory if it doesn't exist
bool exists = System.IO.Directory.Exists(Path.GetDirectoryName(path));
if (!exists)
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path));

if (File.Exists(path))
{
File.Delete(path);
}

// save to file
byteViewer.SaveToFile(path);

// load from file
byteViewer.SetFile(path);

string msg = "Save file to" + path + "is complete";
MessageBox.Show(msg);
}
}
}


1297978.png

Continue reading...
 
Back
Top