My background is embedded programming in the C/C++ world. I have recently joined a project that is using C#. My issue is how do I translate C structs to C# structs when the data within them must remain contiguous to match a stream of data? For example, I have two structs:
typedef struct
{
byte regs[15];
} Registers;
typedef struct
{
Registers Grp1;
Registers Grp2;
} HW_Devices;
From what I have seen, C# wants the array to be dynamically allocated which in this case would make the memory non-contiguous and thus I cannot read the data from the stream directly to the structure. Hopefully I'm missing something simple here.
Converting C arrays to C#
Page 1 of 14 Replies - 446 Views - Last Post: 14 July 2010 - 01:52 PM
Replies To: Converting C arrays to C#
#2
Re: Converting C arrays to C#
Posted 14 July 2010 - 01:47 PM
Why are you trying to convert them? In C# you can focus on being more object oriented. In my limited experience using classes would be the way to go. In object oriented languages structs seem slightly outdated.
#3
Re: Converting C arrays to C#
Posted 14 July 2010 - 01:47 PM
C# supports struct
[StructLayout(LayoutKind.Sequential, Pack = 2)]
public struct EdsCapacity
{
public int NumberOfFreeClusters;
public int BytesPerSector;
public int Reset;
}
#5
Re: Converting C arrays to C#
Posted 14 July 2010 - 01:52 PM
You could try something like this:
More info: http://msdn.microsof...28VS.80%29.aspx
unsafe struct Registers
{
public fixed byte regs[15];
}
More info: http://msdn.microsof...28VS.80%29.aspx
Page 1 of 1
|
|

New Topic/Question
Reply
MultiQuote








|