Click here to Skip to main content
16,005,121 members
Home / Discussions / C#
   

C#

 
QuestionPacking a class into a byte array - would [Serializable] do the trick? Pin
Dewald8-Jun-07 2:02
Dewald8-Jun-07 2:02 
AnswerRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
AikinX8-Jun-07 2:16
AikinX8-Jun-07 2:16 
GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
Dewald8-Jun-07 2:38
Dewald8-Jun-07 2:38 
GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
AikinX8-Jun-07 3:27
AikinX8-Jun-07 3:27 
GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
Dewald8-Jun-07 3:37
Dewald8-Jun-07 3:37 
GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
AikinX8-Jun-07 4:08
AikinX8-Jun-07 4:08 
AnswerRe: Packing a class into a byte array - would [Serializable] do the trick? [modified] Pin
AikinX8-Jun-07 20:43
AikinX8-Jun-07 20:43 
GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
AikinX8-Jun-07 23:50
AikinX8-Jun-07 23:50 
I've done the copying for value object (structure). One thing I can't make is to get a pointer (IntPtr) from reference object (class).

<br />
    public struct myStuct<br />
    {<br />
        public byte myByte1;<br />
        public byte myByte2;<br />
        public ushort myUshort1;<br />
        public ushort myUshort2;<br />
<br />
        private const int MY_SIZE = 6;<br />
        public static byte[] ToByteArray(myStuct source)<br />
        {<br />
            // Allocating an unmanagement memory for marshaling<br />
            IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(MY_SIZE);<br />
            // Copying instance to allocated memory<br />
            System.Runtime.InteropServices.Marshal.StructureToPtr(source, pointer, false);<br />
<br />
            byte[] result = new byte[MY_SIZE];<br />
            // Coping filled memory to byte array<br />
            System.Runtime.InteropServices.Marshal.Copy(pointer, result, 0, MY_SIZE);<br />
<br />
            return result;<br />
        }<br />
<br />
        public static myStuct FromByteArray(byte[] source)<br />
        {<br />
            if (source == null) throw new ArgumentNullException("Source array can not be null");<br />
            if (source.Length != MY_SIZE) throw new ArgumentException("Source array's size has to be " + MY_SIZE + " bytes");<br />
<br />
            // Allocating an unmanagement memory for marshaling<br />
            IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(MY_SIZE);<br />
            // Coping byte array to allocated memory<br />
            System.Runtime.InteropServices.Marshal.Copy(source, 0, pointer, MY_SIZE);<br />
<br />
            // Creating instance from unmanagment memory<br />
            myStuct result = (myStuct)System.Runtime.InteropServices.Marshal.PtrToStructure(pointer, typeof(myStuct));<br />
<br />
            return result;<br />
        }<br />
    }<br />

GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
Dewald10-Jun-07 23:09
Dewald10-Jun-07 23:09 
GeneralRe: Packing a class into a byte array - would [Serializable] do the trick? Pin
AikinX11-Jun-07 0:15
AikinX11-Jun-07 0:15 
QuestionNmericUpDown - set the Value outside the range. HELP!!! Pin
julgri8-Jun-07 1:40
julgri8-Jun-07 1:40 
AnswerRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
Guffa8-Jun-07 1:46
Guffa8-Jun-07 1:46 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
julgri8-Jun-07 6:42
julgri8-Jun-07 6:42 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
Dave Kreskowiak8-Jun-07 6:47
mveDave Kreskowiak8-Jun-07 6:47 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
julgri8-Jun-07 6:56
julgri8-Jun-07 6:56 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
Dave Kreskowiak8-Jun-07 10:08
mveDave Kreskowiak8-Jun-07 10:08 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
julgri8-Jun-07 6:49
julgri8-Jun-07 6:49 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
Dave Kreskowiak8-Jun-07 6:54
mveDave Kreskowiak8-Jun-07 6:54 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
julgri8-Jun-07 7:03
julgri8-Jun-07 7:03 
GeneralRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
Dave Kreskowiak8-Jun-07 10:07
mveDave Kreskowiak8-Jun-07 10:07 
AnswerRe: NmericUpDown - set the Value outside the range. HELP!!! Pin
AikinX9-Jun-07 0:06
AikinX9-Jun-07 0:06 
QuestionString to Byte Array Conversion Pin
M. J. Jaya Chitra8-Jun-07 1:28
M. J. Jaya Chitra8-Jun-07 1:28 
AnswerRe: String to Byte Array Conversion Pin
kristmun8-Jun-07 1:37
kristmun8-Jun-07 1:37 
AnswerRe: String to Byte Array Conversion Pin
Sathesh Sakthivel8-Jun-07 1:37
Sathesh Sakthivel8-Jun-07 1:37 
GeneralRe: String to Byte Array Conversion Thank you Pin
M. J. Jaya Chitra8-Jun-07 1:46
M. J. Jaya Chitra8-Jun-07 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.