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 / C++ / MFC
  4. Unix to WIndows Message

Unix to WIndows Message

Scheduled Pinned Locked Moved C / C++ / MFC
c++
5 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.
  • A Offline
    A Offline
    Andy202
    wrote on last edited by
    #1

    My Windows MFC Application will receive a structure from a Unix machine (Big Endian) over UDP. The structure has the following data types:-

    struct RXData {
    double timeStamp;
    double item1;
    long item2;
    double item3;
    int flag1;
    bool state;
    };

    What do I need to do on the Windows platform to get the data in the correct format; i.e. do I use procedures like theses below - I did try but still not getting the correct answer.

    float swap(float d)
    {
    float a;
    unsigned char *dst = (unsigned char *)&a;
    insigned char *src = (unsigned char *)&d;
    dst[0] = src[3];
    dst[1] = src[2];
    dst[2] = src[1];
    dst[3] = src[0];
    return a;
    }

    short convert_short(short in)
    {
    short out;
    char *p_in = (char *) ∈
    char *p_out = (char *) &out;
    p_out[0] = p_in[1];
    p_out[1] = p_in[0];
    return out;
    }

    The floats give incorrect values. Any suggestions for this simple scheme please.:confused: Regards, Andy.

    L C 2 Replies Last reply
    0
    • A Andy202

      My Windows MFC Application will receive a structure from a Unix machine (Big Endian) over UDP. The structure has the following data types:-

      struct RXData {
      double timeStamp;
      double item1;
      long item2;
      double item3;
      int flag1;
      bool state;
      };

      What do I need to do on the Windows platform to get the data in the correct format; i.e. do I use procedures like theses below - I did try but still not getting the correct answer.

      float swap(float d)
      {
      float a;
      unsigned char *dst = (unsigned char *)&a;
      insigned char *src = (unsigned char *)&d;
      dst[0] = src[3];
      dst[1] = src[2];
      dst[2] = src[1];
      dst[3] = src[0];
      return a;
      }

      short convert_short(short in)
      {
      short out;
      char *p_in = (char *) ∈
      char *p_out = (char *) &out;
      p_out[0] = p_in[1];
      p_out[1] = p_in[0];
      return out;
      }

      The floats give incorrect values. Any suggestions for this simple scheme please.:confused: Regards, Andy.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      you said the struct holds doubles, yet you show a swap function dealing with floats, not doubles? apart from that, everything looks OK. Although there are other schemes, some involving a union. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      R 1 Reply Last reply
      0
      • A Andy202

        My Windows MFC Application will receive a structure from a Unix machine (Big Endian) over UDP. The structure has the following data types:-

        struct RXData {
        double timeStamp;
        double item1;
        long item2;
        double item3;
        int flag1;
        bool state;
        };

        What do I need to do on the Windows platform to get the data in the correct format; i.e. do I use procedures like theses below - I did try but still not getting the correct answer.

        float swap(float d)
        {
        float a;
        unsigned char *dst = (unsigned char *)&a;
        insigned char *src = (unsigned char *)&d;
        dst[0] = src[3];
        dst[1] = src[2];
        dst[2] = src[1];
        dst[3] = src[0];
        return a;
        }

        short convert_short(short in)
        {
        short out;
        char *p_in = (char *) ∈
        char *p_out = (char *) &out;
        p_out[0] = p_in[1];
        p_out[1] = p_in[0];
        return out;
        }

        The floats give incorrect values. Any suggestions for this simple scheme please.:confused: Regards, Andy.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Actually you need to convert doubles, not floats (i.e. 8-byte values instead of 4-byte ones). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • L Luc Pattyn

          you said the struct holds doubles, yet you show a swap function dealing with floats, not doubles? apart from that, everything looks OK. Although there are other schemes, some involving a union. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          Although there are other schemes, some involving a union.

          Which I would normally opt for. :)

          It is a crappy thing, but it's life -^ Carlo Pallini

          C 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Luc Pattyn wrote:

            Although there are other schemes, some involving a union.

            Which I would normally opt for. :)

            It is a crappy thing, but it's life -^ Carlo Pallini

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            I cannot find a 'useful usage' of a union to reverse byte order (while I highly appreciate unions as 'variants'). :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            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