473,378 Members | 1,280 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,378 software developers and data experts.

Casting question

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
8 5021
> 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
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
> 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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
2
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...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.