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. Convert struct to byte[]

Convert struct to byte[]

Scheduled Pinned Locked Moved C#
question
9 Posts 4 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.
  • D Offline
    D Offline
    Divyang Mithaiwala
    wrote on last edited by
    #1

    Hello all, I create one struct now i have to send this data to socket method SentTo. But SendTo method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.


    Divyang Mithaiwala System Engineer & Software Developer

    L A M 3 Replies Last reply
    0
    • D Divyang Mithaiwala

      Hello all, I create one struct now i have to send this data to socket method SentTo. But SendTo method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.


      Divyang Mithaiwala System Engineer & Software Developer

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      You will have to write a custom method that does it. We may help you achieving this, if you post the code for your struct. Also, do you have control of both communication endpoints? -------- "I say no to drugs, but they don't listen." - Marilyn Manson

      D 1 Reply Last reply
      0
      • L Le centriste

        You will have to write a custom method that does it. We may help you achieving this, if you post the code for your struct. Also, do you have control of both communication endpoints? -------- "I say no to drugs, but they don't listen." - Marilyn Manson

        D Offline
        D Offline
        Divyang Mithaiwala
        wrote on last edited by
        #3

        Hello Michel, Check your mail. or click here[^]. Thanx for responcing.


        Divyang Mithaiwala System Engineer & Software Developer

        L 1 Reply Last reply
        0
        • D Divyang Mithaiwala

          Hello Michel, Check your mail. or click here[^]. Thanx for responcing.


          Divyang Mithaiwala System Engineer & Software Developer

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          Huh? -------- "I say no to drugs, but they don't listen." - Marilyn Manson

          D 1 Reply Last reply
          0
          • L Le centriste

            Huh? -------- "I say no to drugs, but they don't listen." - Marilyn Manson

            D Offline
            D Offline
            Divyang Mithaiwala
            wrote on last edited by
            #5

            I send you the whole code on your email address. Plz collect it from there.


            Divyang Mithaiwala System Engineer & Software Developer

            1 Reply Last reply
            0
            • D Divyang Mithaiwala

              Hello all, I create one struct now i have to send this data to socket method SentTo. But SendTo method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.


              Divyang Mithaiwala System Engineer & Software Developer

              A Offline
              A Offline
              Andy Moore
              wrote on last edited by
              #6

              Here's how.

              public struct MyStruct
              {
              public int x;
              public int y;
              public float z;
              }

              IntPtr p;
              MyStruct s;
              int size;
              byte[] buffer;

              s = new MyStruct();

              s.x = 1;
              s.y = 2;
              s.z = 3.0f;

              size = sizeof( typeof( MyStruct ) );

              p = Marshal.AllocHGlobal( size );

              Marshal.PtrToStructure( s, p, true );
              buffer = new byte[ size ];

              Marshal.Copy( p, buffer, 0, size );

              Marshal.FreeHGlobal( p );

              I hope this helps. Deus caritas est

              A 1 Reply Last reply
              0
              • A Andy Moore

                Here's how.

                public struct MyStruct
                {
                public int x;
                public int y;
                public float z;
                }

                IntPtr p;
                MyStruct s;
                int size;
                byte[] buffer;

                s = new MyStruct();

                s.x = 1;
                s.y = 2;
                s.z = 3.0f;

                size = sizeof( typeof( MyStruct ) );

                p = Marshal.AllocHGlobal( size );

                Marshal.PtrToStructure( s, p, true );
                buffer = new byte[ size ];

                Marshal.Copy( p, buffer, 0, size );

                Marshal.FreeHGlobal( p );

                I hope this helps. Deus caritas est

                A Offline
                A Offline
                Andy Moore
                wrote on last edited by
                #7

                Sorry, the Marshal.PtrToStructure should be Marshal.StructureToPtr. Deus caritas est

                D 1 Reply Last reply
                0
                • A Andy Moore

                  Sorry, the Marshal.PtrToStructure should be Marshal.StructureToPtr. Deus caritas est

                  D Offline
                  D Offline
                  Divyang Mithaiwala
                  wrote on last edited by
                  #8

                  Greate. Good. I will help me throught out my project. Thanx Andy.


                  Divyang Mithaiwala System Engineer & Software Developer

                  1 Reply Last reply
                  0
                  • D Divyang Mithaiwala

                    Hello all, I create one struct now i have to send this data to socket method SentTo. But SendTo method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.


                    Divyang Mithaiwala System Engineer & Software Developer

                    M Offline
                    M Offline
                    mcljava
                    wrote on last edited by
                    #9

                    I deal with packets to byte streams quite frequently in client/server applications. Packets are essentially data structures with an assortment of binary and ascii or whatever formatted data. Here are a couple functions you could use, and polymorhisize as you need: public int Encode( int i32Value, ref byte[] pdu, int off ) { byte[] byte32Int = new byte[ cBytesPerWord32 ]; int netValue = System.Net.IPAddress.HostToNetworkOrder( i32Value ); byte32Int = BitConverter.GetBytes( netValue ); Array.Copy( byte32Int, 0, pdu, off, cBytesPerWord32 ); return cBytesPerWord32; } public int Decode( ref byte[] pdu, int off, ref uint u32Value ) { uint netValue = BitConverter.ToUInt32( pdu, off ); u32Value = (uint)System.Net.IPAddress.NetworkToHostOrder( (int) netValue ); return cBytesPerWord32; } Although the function above only deals with 32-bit uint's you can imagine how easy it is to add support for 16-bit and 64-bit integers/unsigned integers. Strings are variable length in most cases otherwise a simple Array.Copy() would suffice. So if you have to pass strings, at a minimum you'll be best served with a length byte and quite possible some sort of a type or tag byte - especialy if you have lots of string elements. That way you can handle strings in any order. Good luck Mike Luster CTI/IVR/Telephony SME

                    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