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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Convert Struct

Convert Struct

Scheduled Pinned Locked Moved Managed C++/CLI
tutorial
6 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.
  • J Offline
    J Offline
    javad_2005
    wrote on last edited by
    #1

    Hi how to Convert Struct To Byte

    P 1 Reply Last reply
    0
    • J javad_2005

      Hi how to Convert Struct To Byte

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      What do you mean by convert ?


      Prasad MS MVP -  VC++

      J 1 Reply Last reply
      0
      • P prasad_som

        What do you mean by convert ?


        Prasad MS MVP -  VC++

        J Offline
        J Offline
        javad_2005
        wrote on last edited by
        #3

        Socket ^s; s->Send(byte......... I want to send struct

        M 1 Reply Last reply
        0
        • J javad_2005

          Socket ^s; s->Send(byte......... I want to send struct

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          The simplest method would be to create a Byte array the size of the structure and copy the structure bytes to the array.

          MyStruct mystruct;
              array<Byte>^ ByteArray = gcnew array<Byte>(sizeof(MyStruct));
              for (int i = 0; i < sizeof(MyStruct); i++)
                  ByteArray[i] = ((unsigned char *)&mystruct)[i];
              Socket ^s;
              s->Send(ByteArray);

          There's other problems which may or may not be a factor in your situation See here[^] A more robust solution is to make classes/structs serializable so the conversion to/from a Byte array is built in to the class. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J 1 Reply Last reply
          0
          • M Mark Salsbery

            The simplest method would be to create a Byte array the size of the structure and copy the structure bytes to the array.

            MyStruct mystruct;
                array<Byte>^ ByteArray = gcnew array<Byte>(sizeof(MyStruct));
                for (int i = 0; i < sizeof(MyStruct); i++)
                    ByteArray[i] = ((unsigned char *)&mystruct)[i];
                Socket ^s;
                s->Send(ByteArray);

            There's other problems which may or may not be a factor in your situation See here[^] A more robust solution is to make classes/structs serializable so the conversion to/from a Byte array is built in to the class. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            J Offline
            J Offline
            javad_2005
            wrote on last edited by
            #5

            Dont work my struct is manage ref Struct MyStruct { } but sizeof dont work for manage Struct

            M 1 Reply Last reply
            0
            • J javad_2005

              Dont work my struct is manage ref Struct MyStruct { } but sizeof dont work for manage Struct

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Oh :)  In that case you should probably use serialization. Serializing Objects (.NET)[^] You can use .NET serialization (like Binary Serialization[^]) or you could serialize the struct yourself by writing whatever bytes make up the structure. .NET serialization is robust and has the bonus of working with other .NET languages. If the data is being sent to non-.NET apps then you'll need to write custom serialization. Regardless of the method you use, you need a array of bytes - you can fill that array however you want, as long as there's enough info there for the receiving end to reconstruct the structure. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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