By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,574 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,574 IT Pros & Developers. It's quick & easy.

Casting question

P: n/a
tom
Hi All,

I'm stuck whit an issue I can't seem to resolve in C#:

I have an arry of bytes which I would like to "recast" to an array of
structs with an Explicit layout. I tried the Buffer.BlockCopy method, but
that one complains my struct is not a primitive type.

Any Suggestions ?

In detail:
my struct contains an int and a byte (so nothing special like arrays or so
;-), you can maybe best compare it with a point structure)

The array of bytes is... just an arry of bytes.

Ideally what I would like to do is assign the address of the last one to the
address of the first one. But C# doesn't like that anymore (?).

So I tried Buffer.BlockCopy, but as stated before that doesn't seem to work
because my struct is not a primitive type.

So Plan B. Using unsafe code and a fixed statement: I was able to iterate
through my byte array and cast/copy every struct item one by one. Altough it
did work, I don't really like this solution as it is consuming quite some
time (large array) while the memory contents are just identical for both.

Can somebody help me out on this.
Mar 2 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> Hi All,

I'm stuck whit an issue I can't seem to resolve in C#:

I have an arry of bytes which I would like to "recast" to an array of
structs with an Explicit layout. I tried the Buffer.BlockCopy method,
but that one complains my struct is not a primitive type.

<snip>

Look at the Marshal class.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 2 '06 #2

P: n/a
tom
I did have look at the Marshal.PtrToStructure Class, but it only allows me to
transfor 1 struct a a time, not an array of structs at once.

Maybe there are some tricks I don't know about Marshal yet ?

Kindest regrads,

Tom

"Lasse Vågsæther Karlsen" wrote:
Hi All,

I'm stuck whit an issue I can't seem to resolve in C#:

I have an arry of bytes which I would like to "recast" to an array of
structs with an Explicit layout. I tried the Buffer.BlockCopy method,
but that one complains my struct is not a primitive type.

<snip>

Look at the Marshal class.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Mar 2 '06 #3

P: n/a
> I did have look at the Marshal.PtrToStructure Class, but it only
allows me to transfor 1 struct a a time, not an array of structs at
once.

Maybe there are some tricks I don't know about Marshal yet ?

Kindest regrads,


I think you need to marshal them one at a time. Perhaps someone with better
knowledge of the Marshal class can correct me?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 2 '06 #4

P: n/a
You may find the following article helpful. While it deals with reading
structures from binary files (streams), a stream is almost the same as an
array of bytes:

http://csharp.codenewbie.com/article...es-Page_1.html

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:C4**********************************@microsof t.com...
Hi All,

I'm stuck whit an issue I can't seem to resolve in C#:

I have an arry of bytes which I would like to "recast" to an array of
structs with an Explicit layout. I tried the Buffer.BlockCopy method, but
that one complains my struct is not a primitive type.

Any Suggestions ?

In detail:
my struct contains an int and a byte (so nothing special like arrays or so
;-), you can maybe best compare it with a point structure)

The array of bytes is... just an arry of bytes.

Ideally what I would like to do is assign the address of the last one to
the
address of the first one. But C# doesn't like that anymore (?).

So I tried Buffer.BlockCopy, but as stated before that doesn't seem to
work
because my struct is not a primitive type.

So Plan B. Using unsafe code and a fixed statement: I was able to iterate
through my byte array and cast/copy every struct item one by one. Altough
it
did work, I don't really like this solution as it is consuming quite some
time (large array) while the memory contents are just identical for both.

Can somebody help me out on this.

Mar 2 '06 #5

P: n/a
Hi,

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:e7**************@tk2msftngp13.phx.gbl...
You may find the following article helpful. While it deals with reading
structures from binary files (streams), a stream is almost the same as an
array of bytes:

As a matter of fact you have MemoryStream which has one constructor that
receive a byte[]

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 2 '06 #6

P: n/a
tom
Hi,

I included some code:

/*
Sample is a Struct define like this:

[StructLayout(LayoutKind.Explicit,Pack=1,Size=5)]
public struct Sample
{
[FieldOffset(0)] public UInt32 Tau;
[FieldOffset(4)] public Byte Value;
}
*/

_tempBuffer = new Sample[mSimBuffer[_bufferToUse].Length/5];
byte[] Bytes = (byte[])mSimBuffer[_bufferToUse];

//This works, but will convert only 1
GCHandle _hnd =
GCHandle.Alloc(mSimBuffer[_bufferToUse],GCHandleType.Pinned);
Object temp =
Marshal.PtrToStructure(_hnd.AddrOfPinnedObject(),t ypeof(Sample));
_hnd.Free();
//This will throw an error: "The specified structure must be blittable
or have layout information."
//Actually I don't really understand this one: didn't I provide enough
layout info in my struct def?
GCHandle _hnd2 = GCHandle.Alloc(_tempBuffer,GCHandleType.Pinned);
Marshal.StructureToPtr(Bytes,_hnd2.AddrOfPinnedObj ect(),false);
_hnd2.Free();

"Lasse Vågsæther Karlsen" wrote:
I did have look at the Marshal.PtrToStructure Class, but it only
allows me to transfor 1 struct a a time, not an array of structs at
once.

Maybe there are some tricks I don't know about Marshal yet ?

Kindest regrads,


I think you need to marshal them one at a time. Perhaps someone with better
knowledge of the Marshal class can correct me?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Mar 2 '06 #7

P: n/a
tom
Hi Kevin,

Thanks for the reply, using the Marshal.Copy method I was able to get it
working.

Do you know if this Copy method will actually copy every byte to another
memory location, or just set the pointers to the same address ?
the reason I'm asking this is that I need real good performance...

Kindest regards,

Tom

"Kevin Spencer" wrote:
You may find the following article helpful. While it deals with reading
structures from binary files (streams), a stream is almost the same as an
array of bytes:

http://csharp.codenewbie.com/article...es-Page_1.html

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:C4**********************************@microsof t.com...
Hi All,

I'm stuck whit an issue I can't seem to resolve in C#:

I have an arry of bytes which I would like to "recast" to an array of
structs with an Explicit layout. I tried the Buffer.BlockCopy method, but
that one complains my struct is not a primitive type.

Any Suggestions ?

In detail:
my struct contains an int and a byte (so nothing special like arrays or so
;-), you can maybe best compare it with a point structure)

The array of bytes is... just an arry of bytes.

Ideally what I would like to do is assign the address of the last one to
the
address of the first one. But C# doesn't like that anymore (?).

So I tried Buffer.BlockCopy, but as stated before that doesn't seem to
work
because my struct is not a primitive type.

So Plan B. Using unsafe code and a fixed statement: I was able to iterate
through my byte array and cast/copy every struct item one by one. Altough
it
did work, I don't really like this solution as it is consuming quite some
time (large array) while the memory contents are just identical for both.

Can somebody help me out on this.


Mar 2 '06 #8

P: n/a
Marshal.Copy copies data from managed memory to unmanaged memory, and vice
versa.

"to*@vandeplas.com" wrote:
Hi Kevin,

Thanks for the reply, using the Marshal.Copy method I was able to get it
working.

Do you know if this Copy method will actually copy every byte to another
memory location, or just set the pointers to the same address ?
the reason I'm asking this is that I need real good performance...

Kindest regards,

Tom

"Kevin Spencer" wrote:
You may find the following article helpful. While it deals with reading
structures from binary files (streams), a stream is almost the same as an
array of bytes:

http://csharp.codenewbie.com/article...es-Page_1.html

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"to*@vandeplas.com" <to*************@discussions.microsoft.com> wrote in
message news:C4**********************************@microsof t.com...
Hi All,

I'm stuck whit an issue I can't seem to resolve in C#:

I have an arry of bytes which I would like to "recast" to an array of
structs with an Explicit layout. I tried the Buffer.BlockCopy method, but
that one complains my struct is not a primitive type.

Any Suggestions ?

In detail:
my struct contains an int and a byte (so nothing special like arrays or so
;-), you can maybe best compare it with a point structure)

The array of bytes is... just an arry of bytes.

Ideally what I would like to do is assign the address of the last one to
the
address of the first one. But C# doesn't like that anymore (?).

So I tried Buffer.BlockCopy, but as stated before that doesn't seem to
work
because my struct is not a primitive type.

So Plan B. Using unsafe code and a fixed statement: I was able to iterate
through my byte array and cast/copy every struct item one by one. Altough
it
did work, I don't really like this solution as it is consuming quite some
time (large array) while the memory contents are just identical for both.

Can somebody help me out on this.


Mar 2 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.