Search:
Namespaces
Discussions
.NET v1.1
Feedback
Get Bytes of a Structure
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Chandra (VIP)
I come from C++ and as you might know we can just typecast a structure as
char* and start reading chars using that pointer. How can I make a byte[] out
of a C# struct or class object?
Thanks
Chandra
Reply to this message...
Marc Lewandowski
Chandra,
I think the only way to do what you want to do involves using "unsafe" code
blocks and manipulating the pointers directly. Check out the docs on unsafe
code in MSDN.
-Marc
"Chandra" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Imran Koradia (VIP)
something like this (in VB - shouldn't be hard to convert to C#):
(*untested*)
Imports System.Runtime.InteropServices
dim struct as myStructure
dim ptr as
IntPtr
=
Marshal
.AllocHGlobal(SizeOf(struct))
Marshal
.StructurePtr(struct, ptr, True)
dim bytearray(SizeOf(struct)-1) as Byte
For i as Integer = 0 to SizeOf(struct)-1
bytearray(i) =
Marshal
.ReadByte(ptr, i)
Next i
note that the above gives you a byte array. If you need a character array,
you'll need to use the function
Marshal
.ReadInt16 instead.
hope that gets you going in the right direction..
Imran.
"Chandra" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Chandra (VIP)
Imran,
Looks like the code snippet you suggested has to be declared as unsafe and
also you only can take the sizeof of an unmanaged structure. But my structure
is managed.
I basically need to convert the structure to bytes so that I can pass it to
an Encrypt method. And when I decrypt I got to go back to the structure from
the bytes again.
I have tried serializing the object to a memory stream witha bytes array as
a memory store. But for a structure of 10 bytes, 150 bytes were getting used
if I serialize it in binary format. This is a problem for us.
Chandra
"Imran Koradia" wrote:
[Original message clipped]
Reply to this message...
Thomas Scheidegger [MVP] (VIP)
[Original message clipped]
it always includes metadata.
[Original message clipped]
try my helper functions:
RawSerializeEx & RawDeserializeEx :
http://groups.google.com/groups?selm=e10
%24YitCCHA.2296%40tkmsftngp05
--
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html
- http://dnetmaster.net/
Reply to this message...
Imran Koradia (VIP)
> Looks like the code snippet you suggested has to be declared as unsafe
That is correct. The memory is allocated from the unmanaged memory of the
process.
> also you only can take the sizeof of an unmanaged structure.
That's not entirely true - SizeOf (or
Marshal
.SizeOf) can take any object -
managed or unmanaged - but will only return the unmanaged size of the
object - in this case, the unmanaged size of the structure (without any
metadata that might be there.)
[Original message clipped]
Since you didn't mention the purpose, I just came up with something as fast
as I could.
[Original message clipped]
try these links:
http://www.dotnetinterop.com/faq/?q=StructToByteArray
http://dotnetjunkies.com/WebLog/chris.taylor/articles/9016.aspx
http://dotnetjunkies.com/WebLog/chris.taylor/articles/5379.aspx
Imran.
Reply to this message...
Imran Koradia (VIP)
Take a look at the System.Runtime.InteropServices.
Marshal
class; it has
several methods to manipulate unmanaged memory. Specifically, look at the
StructureToPtr method and the ReadByte method.
hope that helps..
Imran.
"Chandra" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
System.IntPtr
System.Runtime.InteropServices.Marshal
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com