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. Structure converstion to Bytearray

Structure converstion to Bytearray

Scheduled Pinned Locked Moved C#
questioncsharp
12 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.
  • S San 0

    Hi All, I have a struct in C# application used for serial port communication. I have to read the struct value send from C# application to a C application. What is to be done??? Thanks in Advance San

    L Offline
    L Offline
    lisan_al_ghaib
    wrote on last edited by
    #3

    You are running serial port communication between 2 managed program?

    S 1 Reply Last reply
    0
    • L lisan_al_ghaib

      You are running serial port communication between 2 managed program?

      S Offline
      S Offline
      San 0
      wrote on last edited by
      #4

      C# program is wrting the data. C program is reading the data. Regards, Sangeetha

      L 1 Reply Last reply
      0
      • S San 0

        C# program is wrting the data. C program is reading the data. Regards, Sangeetha

        L Offline
        L Offline
        lisan_al_ghaib
        wrote on last edited by
        #5

        okey then the best way to do this is to define a sort of a 'communication protocol' between two devices : First byte : message lenght (or data length) data bytes : your data byte that holds checksum [data length][.....data.....][checksum] then in your C# program you have to create a class that build the message you have to send : calculate length convert your structure on a data array (*) calculate the checksum -> So here we are in the core of your question: the best way is to loop over EACH element of your strucuture convert it into byte array and the add it to data array use BitConverter class : BitConverter.GetBytes (T) -> T is an elementary type (float, double, char, int, etc...)

        S 1 Reply Last reply
        0
        • L lisan_al_ghaib

          okey then the best way to do this is to define a sort of a 'communication protocol' between two devices : First byte : message lenght (or data length) data bytes : your data byte that holds checksum [data length][.....data.....][checksum] then in your C# program you have to create a class that build the message you have to send : calculate length convert your structure on a data array (*) calculate the checksum -> So here we are in the core of your question: the best way is to loop over EACH element of your strucuture convert it into byte array and the add it to data array use BitConverter class : BitConverter.GetBytes (T) -> T is an elementary type (float, double, char, int, etc...)

          S Offline
          S Offline
          San 0
          wrote on last edited by
          #6

          Could u please send any sample code or link to imlement this. I am a beginner to this Regards, San

          J L 2 Replies Last reply
          0
          • S San 0

            Could u please send any sample code or link to imlement this. I am a beginner to this Regards, San

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #7

            San wrote:

            I am a beginner to this

            Then start with something simpler, like Hello World. If however you're being paid to do this I suggest you go to your employer and tell them you are not yet capable of the task you have been set.

            L 1 Reply Last reply
            0
            • S San 0

              Could u please send any sample code or link to imlement this. I am a beginner to this Regards, San

              L Offline
              L Offline
              lisan_al_ghaib
              wrote on last edited by
              #8

              let suppose your have this Class :

              class SomeClass
              {
              double l;
              double [] m = new double [10]; //double array
              string s;

              byte [] ToByteArray ()
              {
              List<byte> b = new List<byte> ();
              b.AddRange (BitConverter.GetBytes (l));
              for (int i = 0; i < m.Length ; i++)
              b.AddRange (BitConverter.GetBytes (m [i]));
              char [] tabc = s.ToCharArray();
              for ( int i = 0 ; i < m.Lenght ; i++)
              b.AddRange (BitConverter.GetBytes (tabc [i]);

                return  b.ToArray();
              

              }
              }
              //usage :
              SomeClass c = new SomeClass ();
              byte [] b = c.ToByteArray();

              S 1 Reply Last reply
              0
              • J J4amieC

                San wrote:

                I am a beginner to this

                Then start with something simpler, like Hello World. If however you're being paid to do this I suggest you go to your employer and tell them you are not yet capable of the task you have been set.

                L Offline
                L Offline
                lisan_al_ghaib
                wrote on last edited by
                #9

                Hello ! Don't be so Rude :) we all were beginners some time !

                J 1 Reply Last reply
                0
                • L lisan_al_ghaib

                  Hello ! Don't be so Rude :) we all were beginners some time !

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #10

                  Yes, and we started with beginner-ish tasks, not in-depth like this sort of thing.

                  L 1 Reply Last reply
                  0
                  • J J4amieC

                    Yes, and we started with beginner-ish tasks, not in-depth like this sort of thing.

                    L Offline
                    L Offline
                    lisan_al_ghaib
                    wrote on last edited by
                    #11

                    +1 dude !

                    1 Reply Last reply
                    0
                    • L lisan_al_ghaib

                      let suppose your have this Class :

                      class SomeClass
                      {
                      double l;
                      double [] m = new double [10]; //double array
                      string s;

                      byte [] ToByteArray ()
                      {
                      List<byte> b = new List<byte> ();
                      b.AddRange (BitConverter.GetBytes (l));
                      for (int i = 0; i < m.Length ; i++)
                      b.AddRange (BitConverter.GetBytes (m [i]));
                      char [] tabc = s.ToCharArray();
                      for ( int i = 0 ; i < m.Lenght ; i++)
                      b.AddRange (BitConverter.GetBytes (tabc [i]);

                        return  b.ToArray();
                      

                      }
                      }
                      //usage :
                      SomeClass c = new SomeClass ();
                      byte [] b = c.ToByteArray();

                      S Offline
                      S Offline
                      San 0
                      wrote on last edited by
                      #12

                      thankyou so much

                      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