Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to convert struct to byte array?

How to convert struct to byte array?

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chandru___
    wrote on last edited by
    #1

    Hi Find below the structure am using struct Test { int i; string s; } struct Check { int intI; Test []structArray; } How to convert the Check structure to byte array ? Thanks in advance chandru

    G V 2 Replies Last reply
    0
    • C chandru___

      Hi Find below the structure am using struct Test { int i; string s; } struct Check { int intI; Test []structArray; } How to convert the Check structure to byte array ? Thanks in advance chandru

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      That depends on what you want the byte array to contain. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • C chandru___

        Hi Find below the structure am using struct Test { int i; string s; } struct Check { int intI; Test []structArray; } How to convert the Check structure to byte array ? Thanks in advance chandru

        V Offline
        V Offline
        VSush
        wrote on last edited by
        #3

        The following code converts the structure to bytearray. Hope this helps... using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential, Pack=1)] public struct Test { public int i; public string s; } [StructLayout(LayoutKind.Sequential, Pack=1)] public struct Check { public int I; public Test [] structArray ; public int GetMySize() { int iSize = Marshal.SizeOf( I) + Marshal.SizeOf( typeof( Test)) * structArray.Length; return iSize; } public void MarshalStructToPtr(ref IntPtr ptr) { Marshal.StructureToPtr(I ,ptr, true); ptr = (IntPtr)((int)ptr + Marshal.SizeOf( I )); for( int count = 0; count < structArray.Length; count ++ ) { Marshal.StructureToPtr(structArray[count],ptr, true); ptr = (IntPtr)((int)ptr + Marshal.SizeOf( structArray[count] )); } } } class ConvertStructToByteArray { public void Convert() { Test test1 = new Test(); test1.i = 2; test1.s = "test"; Check objCheck = new Check(); objCheck.I =10; objCheck.structArray = new Test[1]; objCheck.structArray[0] = test1; int Size = objCheck.GetMySize() ; IntPtr pCheck = Marshal.AllocHGlobal((int)Size ); IntPtr Temp = pCheck; objCheck.MarshalStructToPtr( ref Temp); byte[] bytearrCheck = new byte[ Size]; Marshal.Copy( pCheck , bytearrCheck , 0,(int)Size); Marshal.FreeHGlobal( pCheck ); } }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups