4 Replies - 603 Views - Last Post: 14 July 2010 - 01:52 PM Rate Topic: -----

#1 Guest_Joe*


Reputation:

Converting C arrays to C#

Posted 14 July 2010 - 01:40 PM

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.

Is This A Good Question/Topic? 0

Replies To: Converting C arrays to C#

#2 Smurphy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 367
  • Joined: 07-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

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;
        }


Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Converting C arrays to C#

Posted 14 July 2010 - 01:48 PM

Take a look at this. I believe it will help.
Was This Post Helpful? 0
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Converting C arrays to C#

Posted 14 July 2010 - 01:52 PM

You could try something like this:

unsafe struct Registers
{
    public fixed byte regs[15];
}



More info: http://msdn.microsof...28VS.80%29.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1