Click here to Skip to main content
16,022,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have class like below. And i have byte array. First byte has product length. Based on that product name byte allocation should occur. So i may get dynamic byte array collection. In this case how do i map this and assign values in my class object.
C#
[StructLayout(LayoutKind.Sequential)]
 public class Blob
   {
       #region Properties
       public int ProductLength { get; set; }
       public string ProductName { get; set; }
       public byte ProductVersion { get; set; }
       public byte ProductRevision { get; set; }
       public ushort ProtocolVersion { get; set; }
       public ushort ProductId { get; set; }
       public byte DataCount { get; set; }
       public ProductData ProductData { get; set; }
       #endregion
   }


What I have tried:

I have tried the manual mapping. Is this the right way to do it? Kindlyhelp..
C#
Blob blob = new Blob();

int i = 0;
int j = blob.ProductLength = bytes[i];

for (int count = i+1; count < j; count++)
    blob.ProductName = blob.ProductName+ bytes[count].ToString();
Posted
Updated 24-Feb-17 1:18am
v2
Comments
Richard Deeming 24-Feb-17 10:08am    
What's the string encoding that was used to generate the byte array?

Once you know that, it's a trivial call to GetString(bytes, i, j) on the correct Encoding instance to convert the bytes back to a string.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900