473,503 Members | 11,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting a bit array to a structure

Using VS.Net 2003 - a managed VC++ WinForms app:

If I have an unmanged structure like

typedef struct
{
unsigned char Field1;
unsigned char Field2;
unsigned int Field3: 6;
unsigned int Field4: 2;
} MYSTRUCT;

and a managed byte array btArr that is 3 bytes long, what is the syntax to
convert the byte array to a MYSTRUCT?

MYSTRUCT *ms = (MYSTRUCT*)btArr;

does not work (or compile for that matter).

Thank you,
Joe
Nov 17 '05 #1
6 3376
I don't know about btAttr's definition, however and in worst cases can't you
write a function that does the member-wise copying ?

MYSTRUCT s1;
s1.fld1 = btAttr.fld1;
s1.fld2 = btAttr.fld2;

etc...

--
Elias
"Joe Thompson" <jo************@comcast.net> wrote in message
news:9ZQDb.569966$Tr4.1545085@attbi_s03...
Using VS.Net 2003 - a managed VC++ WinForms app:

If I have an unmanged structure like

typedef struct
{
unsigned char Field1;
unsigned char Field2;
unsigned int Field3: 6;
unsigned int Field4: 2;
} MYSTRUCT;

and a managed byte array btArr that is 3 bytes long, what is the syntax to
convert the byte array to a MYSTRUCT?

MYSTRUCT *ms = (MYSTRUCT*)btArr;

does not work (or compile for that matter).

Thank you,
Joe

Nov 17 '05 #2
Hi - it's a byte array declared like this:

Byte btArray[] = new __gc Byte[3];

I really have a much larger structure and array that I am reading from the
serial port and would prefer to move it to my structure quickly. Also, some
of my fields in the structure are larger than one byte and some smaller
(bit-fields)

Joe

"lallous" <la*****@lgwm.org> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
I don't know about btAttr's definition, however and in worst cases can't you write a function that does the member-wise copying ?

MYSTRUCT s1;
s1.fld1 = btAttr.fld1;
s1.fld2 = btAttr.fld2;

etc...

--
Elias
"Joe Thompson" <jo************@comcast.net> wrote in message
news:9ZQDb.569966$Tr4.1545085@attbi_s03...
Using VS.Net 2003 - a managed VC++ WinForms app:

If I have an unmanged structure like

typedef struct
{
unsigned char Field1;
unsigned char Field2;
unsigned int Field3: 6;
unsigned int Field4: 2;
} MYSTRUCT;

and a managed byte array btArr that is 3 bytes long, what is the syntax to convert the byte array to a MYSTRUCT?

MYSTRUCT *ms = (MYSTRUCT*)btArr;

does not work (or compile for that matter).

Thank you,
Joe


Nov 17 '05 #3
You need to __pin the managed array. Look for that keyword in the docs. Let
me know if the help there isn't enough, and I'll try to post an example.

Ronald Laeremans
Visal C++ team

"Joe Thompson" <jo************@comcast.net> wrote in message
news:9ZQDb.569966$Tr4.1545085@attbi_s03...
Using VS.Net 2003 - a managed VC++ WinForms app:

If I have an unmanged structure like

typedef struct
{
unsigned char Field1;
unsigned char Field2;
unsigned int Field3: 6;
unsigned int Field4: 2;
} MYSTRUCT;

and a managed byte array btArr that is 3 bytes long, what is the syntax to
convert the byte array to a MYSTRUCT?

MYSTRUCT *ms = (MYSTRUCT*)btArr;

does not work (or compile for that matter).

Thank you,
Joe

Nov 17 '05 #4
Hi Ronald

After a few attempts, I think I got it - does this look right

int avail = 1024

Byte btArr[] = new Byte[avail]
ComPort->Read(btArr, 0, avail); // this wants a Byte arra
Byte __pin *p = &btArr[1]; // entire array is now pinne
MYSTRUCT *ms = (MYSTRUCT*)p;

Thank you
Joe
Nov 17 '05 #5
Yes, that looks fine. Make sure you don't keep ms alive (e.g. by passing it
back to the caller of this code block) after *p goes out of scope, otherwise
it will no longer be pinned and could move.

Ronald

"Joe Thompson" <an*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi Ronald,

After a few attempts, I think I got it - does this look right?

int avail = 1024;

Byte btArr[] = new Byte[avail];
ComPort->Read(btArr, 0, avail); // this wants a Byte array
Byte __pin *p = &btArr[1]; // entire array is now pinned
MYSTRUCT *ms = (MYSTRUCT*)p;

Thank you,
Joe

Nov 17 '05 #6
I think that sould have been

&btArr[0];
not
&btArr[1];

Thanks for the help,
Joe

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Yes, that looks fine. Make sure you don't keep ms alive (e.g. by passing it back to the caller of this code block) after *p goes out of scope, otherwise it will no longer be pinned and could move.

Ronald

"Joe Thompson" <an*******@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi Ronald,

After a few attempts, I think I got it - does this look right?

int avail = 1024;

Byte btArr[] = new Byte[avail];
ComPort->Read(btArr, 0, avail); // this wants a Byte array
Byte __pin *p = &btArr[1]; // entire array is now pinned
MYSTRUCT *ms = (MYSTRUCT*)p;

Thank you,
Joe


Nov 17 '05 #7

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

Similar topics

13
1911
by: Christopher Benson-Manica | last post by:
The thread where casting is being discussed motivated me to try this. Let's say you wanted to populate a BankRecord structure (as I defined below) with 17-character record numbers, but with the...
20
2048
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
4
16265
by: Fabrizio | last post by:
Hi I cannot figure why it isn't possible to cast a struct array to an object array. I written a structure like this: public struct Test { private int TestA; private int TestB;
2
1859
by: raza | last post by:
In C++ we can do things like (Header is an instance of C++ structure) Send(m_Sock,(char*)&Header,sizeof(Header),0) in C# we have Socket.Send(byte , int offset, int size, flags) how do i...
61
4502
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
2
2155
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
12
1929
by: candy_init | last post by:
I recently came across the following CAST macro which can cast anything to any other thing: #define CAST(new_type,old_object) (*((new_type *)&old_object)) union { char ch; int i; }...
9
430
by: Roman Mashak | last post by:
Hello, All! I met this code recently on some open source sites. What may be the point of using such construction: typedef struct cmd { unsigned int cmdack; unsigned int code; unsigned int...
11
4672
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.