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.BlockCop y 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.BlockCop y, 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. 8 5075
> 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.BlockCop y 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***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
I did have look at the Marshal.PtrToSt ructure 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.BlockCop y 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***@vk arlsen.no PGP KeyID: 0x2A42A1C2
> I did have look at the Marshal.PtrToSt ructure 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***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
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.m icrosoft.com> wrote in
message news:C4******** *************** ***********@mic rosoft.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.BlockCop y 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.BlockCop y, 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.
Hi,
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:e7******** ******@tk2msftn gp13.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
Hi,
I included some code:
/*
Sample is a Struct define like this:
[StructLayout(La youtKind.Explic it,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.P inned);
Object temp =
Marshal.PtrToSt ructure(_hnd.Ad drOfPinnedObjec t(),typeof(Samp le));
_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,GCH andleType.Pinne d);
Marshal.Structu reToPtr(Bytes,_ hnd2.AddrOfPinn edObject(),fals e);
_hnd2.Free();
"Lasse Vågsæther Karlsen" wrote: I did have look at the Marshal.PtrToSt ructure 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***@vk arlsen.no PGP KeyID: 0x2A42A1C2
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.m icrosoft.com> wrote in message news:C4******** *************** ***********@mic rosoft.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.BlockCop y 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.BlockCop y, 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.
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.m icrosoft.com> wrote in message news:C4******** *************** ***********@mic rosoft.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.BlockCop y 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.BlockCop y, 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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 (perhaps mistakenly) that the
purpose of a void pointer was to cast into a legitimate date type. Is
this wrong? Why, and what is considered to be correct form?
|
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;
}
|
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 from base class, and
casting, derived classes would already have their
variables initialiezed(cause they have already been
initialized in the base class) ....
|
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;
but it complains for the following lines
|
by: Marco |
last post by:
I need to get a iterator from any generic collection.
public class .... GetIterator(Object collection)
{
.....
}
| |
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 field UserType) and for each type of
user a inherited class like SessionMasterUser and SessionNormalUser.
|
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 has similar
structure of the classes in my application. The test worked fine, the
casting I want to do must work. I compared the structure of the test
with the application to ensure they where similar (inheriting twice,
base class implements...
|
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
|
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
NumberVertices instead of Vertices, I can either just use a non-
templated Edge class which stores pointers to my NumberVertices and
leave it up to the programmer to cast back again to NumberVertex (this
type of casting only works with...
|
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{}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |