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. How to send double value as 8 uint8 values?

How to send double value as 8 uint8 values?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structurestutorial
7 Posts 6 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
    cahit23
    wrote on last edited by
    #1

    Hi all, I am currently working on Serial Interface between a control box and PC and want to send/receive double values from Pc to the box as 8 uint8 values since the box only accepts in uint8 form. For that i thought as follows : to get that double value (64 bits) in binary form and get the last 8 bits with &(and) operator and shifting bits to right till all bits are finished,namely : double var; char var2send[8]; % in array var2send[0]= var & 0x00000000000000ff % last 8 bits with & operator var>>8; %shifting bits var2send[1]= var & 0x00000000000000ff var>>8; . . % and sending this array as output . But in this code, i received an error that says i cant use double with & operators so that must be converted 64 bit long form.Besides, cast function rounds my double value to the nearest integer even if i type number like 23.34353 with precision. How can i manage to do this?? or anyone has better way or suggestion? I hope i could clearly explain my problem. I would be very pleased if you could help me! Thanks a lot! Cahit

    D C 2 Replies Last reply
    0
    • C cahit23

      Hi all, I am currently working on Serial Interface between a control box and PC and want to send/receive double values from Pc to the box as 8 uint8 values since the box only accepts in uint8 form. For that i thought as follows : to get that double value (64 bits) in binary form and get the last 8 bits with &(and) operator and shifting bits to right till all bits are finished,namely : double var; char var2send[8]; % in array var2send[0]= var & 0x00000000000000ff % last 8 bits with & operator var>>8; %shifting bits var2send[1]= var & 0x00000000000000ff var>>8; . . % and sending this array as output . But in this code, i received an error that says i cant use double with & operators so that must be converted 64 bit long form.Besides, cast function rounds my double value to the nearest integer even if i type number like 23.34353 with precision. How can i manage to do this?? or anyone has better way or suggestion? I hope i could clearly explain my problem. I would be very pleased if you could help me! Thanks a lot! Cahit

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      This is just a guess, but can you use a union for this?

      union
      {
      double dValue;
      struct
      {
      uint8 uValu1;
      uint8 uValu2;
      uint8 uValu3;
      uint8 uValu4;
      uint8 uValu5;
      uint8 uValu6;
      uint8 uValu7;
      uint8 uValu8;
      }
      };


      "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

      "Judge not by the eye but by the heart." - Native American Proverb

      S 1 Reply Last reply
      0
      • D David Crow

        This is just a guess, but can you use a union for this?

        union
        {
        double dValue;
        struct
        {
        uint8 uValu1;
        uint8 uValu2;
        uint8 uValu3;
        uint8 uValu4;
        uint8 uValu5;
        uint8 uValu6;
        uint8 uValu7;
        uint8 uValu8;
        }
        };


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        or even union { double dValue; uint8 u8value[8]; }; In general, what makes stuff like this tricky is using it across different architectures, such as x86 and m68k, for example, where byte ordering is different.

        Steve S Developer for hire

        D K 2 Replies Last reply
        0
        • S Steve S

          or even union { double dValue; uint8 u8value[8]; }; In general, what makes stuff like this tricky is using it across different architectures, such as x86 and m68k, for example, where byte ordering is different.

          Steve S Developer for hire

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          :doh:


          "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • C cahit23

            Hi all, I am currently working on Serial Interface between a control box and PC and want to send/receive double values from Pc to the box as 8 uint8 values since the box only accepts in uint8 form. For that i thought as follows : to get that double value (64 bits) in binary form and get the last 8 bits with &(and) operator and shifting bits to right till all bits are finished,namely : double var; char var2send[8]; % in array var2send[0]= var & 0x00000000000000ff % last 8 bits with & operator var>>8; %shifting bits var2send[1]= var & 0x00000000000000ff var>>8; . . % and sending this array as output . But in this code, i received an error that says i cant use double with & operators so that must be converted 64 bit long form.Besides, cast function rounds my double value to the nearest integer even if i type number like 23.34353 with precision. How can i manage to do this?? or anyone has better way or suggestion? I hope i could clearly explain my problem. I would be very pleased if you could help me! Thanks a lot! Cahit

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            BYTE *pBytes = (BYTE *)&var; send(pBytes[0]); send(pBytes[1]); ... send(pBytes[7]);

            image processing | blogging

            1 Reply Last reply
            0
            • S Steve S

              or even union { double dValue; uint8 u8value[8]; }; In general, what makes stuff like this tricky is using it across different architectures, such as x86 and m68k, for example, where byte ordering is different.

              Steve S Developer for hire

              K Offline
              K Offline
              kakan
              wrote on last edited by
              #6

              Steve S wrote:

              union { double dValue; uint8 u8value[8]; };

              I would like to suggest this:

              #pragma pack(1)
              union
              {
              double dValue;
              uint8 u8value[8];
              };
              #pragma pack()

              Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

              Z 1 Reply Last reply
              0
              • K kakan

                Steve S wrote:

                union { double dValue; uint8 u8value[8]; };

                I would like to suggest this:

                #pragma pack(1)
                union
                {
                double dValue;
                uint8 u8value[8];
                };
                #pragma pack()

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                There really is no need for that. Unsigned char's will be placed on 8-bit boundaries on all common systems, and doubles will be placed on 64-bit boundaries. The union will be sized and oriented based on the largest size requirement (the double), so packing it serves no purpose.

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                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