10 Replies - 796 Views - Last Post: 20 September 2013 - 05:40 AM Rate Topic: -----

#1 pbshines  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 13

Does C# support struct bit definitions?

Posted 19 September 2013 - 04:33 AM

Does C# support struct bit definitions?.
For example is there a way to declare a structure like this in c#.net?
typedef struct

{

    UINT16 SrcPort;

    UINT16 DstPort;

    UINT32 SeqNum;

    UINT32 AckNum;

    UINT16 Reserved1:4;

    UINT16 HdrLength:4;

    UINT16 Fin:1;

    UINT16 Syn:1;

    UINT16 Rst:1;

    UINT16 Psh:1;

    UINT16 Ack:1;

    UINT16 Urg:1;

    UINT16 Reserved2:2;

    UINT16 Window;

    UINT16 Checksum;

    UINT16 UrgPtr;

} DIVERT_TCPHDR


Please help me to convert this c++ structure to c# structure? It is very urgent.

How can we set bit positions in a C# Structure ?

Is This A Good Question/Topic? 0
  • +

Replies To: Does C# support struct bit definitions?

#2 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 05:21 AM

Have you tried at all? I mean, if you type UInt16 x = 3; into Visual Studio, you're going to find out the answer pretty quick (spoiler: yes, it's there).
Was This Post Helpful? 0
  • +
  • -

#3 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 05:51 AM

You can use the StructLayout attribute to set explicit byte positions.

[StructLayout(LayoutKind.Explicit)]
struct MyStruct
{
	[FieldOffset(0)]
	UInt32 integer1;
	
	[FieldOffset(4)]
	UInt16 integer2;
}

This post has been edited by Witchking: 19 September 2013 - 05:52 AM

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 06:36 AM

But that only gives you byte level layout control. The OP is looking for bit level layout control.

View Postpharylon, on 19 September 2013 - 08:21 AM, said:

Have you tried at all? I mean, if you type UInt16 x = 3; into Visual Studio, you're going to find out the answer pretty quick (spoiler: yes, it's there).


Actually, it's not there in the C# language itself. There is a way to do bit level control if you lean on the CLR.
Was This Post Helpful? 2
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 10:04 AM

You can have bit level control with BitVector32. You can even combine bits using CreateSection to form 2, 3, etc. bit fields.
Was This Post Helpful? 3
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 02:55 PM

Yes, but notice that BitVector32 is not in the C# language specification.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 05:12 PM

Also remember to Pin objects especially if passing them to C++ code, so the garbage collector doesn't move the address of the object. Cos C# is a managed language it is free to move objects around in memory unless you explicitly instruct it not to.
Was This Post Helpful? 0
  • +
  • -

#8 pbshines  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 13

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 10:17 PM

I think bit fields are not possible in c# structure. It is possible in c++.
typedef struct
{
UINT16 SrcPort:16;
UINT16 DstPort:16;
UINT32 SeqNum:32;
UINT32 AckNum:16;
UINT16 Reserved1:4;
UINT16 HdrLength:4;
}IP_HDR

How can we create a structure like this in c# with bit fields?

My question is how we can set bit fields in C# structure.Is there any ways to do it in C#?
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Does C# support struct bit definitions?

Posted 19 September 2013 - 10:27 PM

What about your previous topic on this? Do you think opening multiple topics in which you ask essentially the same thing, is going to endear yourself to the community?
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Does C# support struct bit definitions?

Posted 20 September 2013 - 03:21 AM

Topics merged.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Does C# support struct bit definitions?

Posted 20 September 2013 - 05:40 AM

See Momerath's response. Then a do a search on those terms. Pay close attention to the results from StackOverflow. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1