473,372 Members | 1,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

C Union -> C# struct LayoutKind.Explicit FieldOffset - how?

Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with
LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247referenc.../18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2
structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
4 13885
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with
LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247referenc.../18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2
structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2
Thanks Sijin, i had 1 question.

How do i know what argument to be past in here:

FieldOffset(0) or (1) or (16)

Hmm, can you give me some examples? I can't find proper explanation even on
msdn.

Thanks a lot. :) Really appreciate it.

"Sijin Joseph" wrote:
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with
LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247referenc.../18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2
structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #3
The FieldOffset attribute defines the number of bytes from the start of the
structure where this field will be layed out in memory.

In your case avalue and b value are unsigned chars which take 1 byte so they
will be layed out at mem locations 0 and 1. cvalue is a unsigned short since
it is a union that will also be layed out at offset 0 from the start of the
structure and take 2 bytes.

Basically FieldOffset defines the number of bytes from the start of the
structure where the field will be placed.
Let me know if you need more hlp.
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:24**********************************@microsof t.com...
Thanks Sijin, i had 1 question.

How do i know what argument to be past in here:

FieldOffset(0) or (1) or (16)

Hmm, can you give me some examples? I can't find proper explanation even on msdn.

Thanks a lot. :) Really appreciate it.

"Sijin Joseph" wrote:
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
Hi there,

I had a union in C:

typedef union TagMessage
{
struct {
unsigned char avalue;
unsigned char bvalue;
} a;
struct {
unsigned short cvalue;
} b;
} Message;

From these few links, there is a possible way is to declare a struct with LayoutKind.Explict and use FieldOffset.

http://www.dotnet247.com/247referenc.../18/93017.aspx
http://www.hanselman.com/blog/PermaLink.aspx?guid=187

The problem is how do i relate the FieldOffset in my scenarios. I had 2 structs in my C union. How do i achieve it with the help of that?

Any idea please? Thanks.
--
Regards,
Chua Wen Ching :)


Nov 16 '05 #4
Hi Sijin,

Thanks a lot for the explanation. Ok, i will give it a try when i get back
to office, i hope it will work.

Thanks again.

"Sijin Joseph" wrote:
The FieldOffset attribute defines the number of bytes from the start of the
structure where this field will be layed out in memory.

In your case avalue and b value are unsigned chars which take 1 byte so they
will be layed out at mem locations 0 and 1. cvalue is a unsigned short since
it is a union that will also be layed out at offset 0 from the start of the
structure and take 2 bytes.

Basically FieldOffset defines the number of bytes from the start of the
structure where the field will be placed.
Let me know if you need more hlp.
--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:24**********************************@microsof t.com...
Thanks Sijin, i had 1 question.

How do i know what argument to be past in here:

FieldOffset(0) or (1) or (16)

Hmm, can you give me some examples? I can't find proper explanation even

on
msdn.

Thanks a lot. :) Really appreciate it.

"Sijin Joseph" wrote:
Try this

[StructLayout(LayoutKind.Explicit)]
public struct Message
{
[StructLayout(LayoutKind.Explicit)]
public struct AStruct
{
[FieldOffset(0)] public System.Byte avalue;
[FieldOffset(1)] public System.Byte bvalue;
}

[FieldOffset(0)]public AStruct a;

[StructLayout(LayoutKind.Explicit)]
public struct BStruct
{
[FieldOffset(0)] public System.UInt16 cvalue;
}

[FieldOffset(0)]public BStruct b;
}

--
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:73**********************************@microsof t.com...
> Hi there,
>
> I had a union in C:
>
> typedef union TagMessage
> {
> struct {
> unsigned char avalue;
> unsigned char bvalue;
> } a;
> struct {
> unsigned short cvalue;
> } b;
> } Message;
>
> From these few links, there is a possible way is to declare a struct with > LayoutKind.Explict and use FieldOffset.
>
> http://www.dotnet247.com/247referenc.../18/93017.aspx
> http://www.hanselman.com/blog/PermaLink.aspx?guid=187
>
> The problem is how do i relate the FieldOffset in my scenarios. I had 2 > structs in my C union. How do i achieve it with the help of that?
>
> Any idea please? Thanks.
> --
> Regards,
> Chua Wen Ching :)


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
4
by: Girish | last post by:
I have 2 differesnt defination of same Union as below and a piece of code for printing size of Union and its members.. union U { union U { int i; int j; }a;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
10
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.