.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification