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 do I pack to integer values in a double ?

How do I pack to integer values in a double ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
13 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.
  • T toxcct

    what about using union ?

    union U {
    double _d;
    int _i[2];
    };


    TOXCCT >>> GEII power

    [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

    A Offline
    A Offline
    Astricks
    wrote on last edited by
    #3

    Thank you for your reply sir, But I do not want an union :(. The matter is, You have a function like this. Imagine you cannot change it's params. It's built by someone and it's used in numerous instances. void testDouble(double d) { cout< Now you say , testDouble(2143250); it prints 2143250. Now I want to send two integer values to this function for other reason. I have two numbres, 1000, 2000. these are ints so takes only 4 bytes. `double 00000000000000000000.....64 now 0000000..32 | 0000.......32 is my idea.` But how do I do that? So that in the function, I can do like, void testDouble(double d) { cout<<(HIGHER32)d; cout<<(LOWER32)d; } get me now?

    T C 2 Replies Last reply
    0
    • A Astricks

      Thank you for your reply sir, But I do not want an union :(. The matter is, You have a function like this. Imagine you cannot change it's params. It's built by someone and it's used in numerous instances. void testDouble(double d) { cout< Now you say , testDouble(2143250); it prints 2143250. Now I want to send two integer values to this function for other reason. I have two numbres, 1000, 2000. these are ints so takes only 4 bytes. `double 00000000000000000000.....64 now 0000000..32 | 0000.......32 is my idea.` But how do I do that? So that in the function, I can do like, void testDouble(double d) { cout<<(HIGHER32)d; cout<<(LOWER32)d; } get me now?

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #4

      wait, you want to merge 2 ints into a double, or split a double into 2 ints ? BTW be careful, sizeof(int) == 4 only on 32 bits plateforms !


      TOXCCT >>> GEII power

      [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

      A 1 Reply Last reply
      0
      • T toxcct

        wait, you want to merge 2 ints into a double, or split a double into 2 ints ? BTW be careful, sizeof(int) == 4 only on 32 bits plateforms !


        TOXCCT >>> GEII power

        [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

        A Offline
        A Offline
        Astricks
        wrote on last edited by
        #5

        Yes exactly, but I'm happy that sizeof(int) is 4 :) that enough to keep a 4 bytes data :laugh:

        toxcct wrote:

        you want to merge 2 ints into a double, or split a double into 2 ints ?

        Yes. I want this. May be you can try with shift operators :rolleyes:

        1 Reply Last reply
        0
        • A Astricks

          Thank you for your reply sir, But I do not want an union :(. The matter is, You have a function like this. Imagine you cannot change it's params. It's built by someone and it's used in numerous instances. void testDouble(double d) { cout< Now you say , testDouble(2143250); it prints 2143250. Now I want to send two integer values to this function for other reason. I have two numbres, 1000, 2000. these are ints so takes only 4 bytes. `double 00000000000000000000.....64 now 0000000..32 | 0000.......32 is my idea.` But how do I do that? So that in the function, I can do like, void testDouble(double d) { cout<<(HIGHER32)d; cout<<(LOWER32)d; } get me now?

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

          int main(int argc, char* argv[])
          {
          __int32 i[2];
          i[0] = 1000;
          i[1] = 2000;

          double d = 0;
          d = \*(double\*)&i\[0\];
          
          printf("%f\\n", d);
          
          \_\_int32 \*o;
          o = (\_\_int32\*)&d;
          printf("%d %d\\n", o\[0\], o\[1\]);
          
          return 0;
          

          }

          of course, anyone doing something like this would end up in the Daily WTF, for sure.

          image processing | blogging

          A 2 Replies Last reply
          0
          • A Astricks

            How do I pack two integer values in a double ? i.e int x=1000; int y=2000; double z; now I want the first four bytes to be occupied by x and the next 4 by y in Z How do I do that? any shift operators would help? please tell me how do I pack & unpack it?

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

            Is this what you are after:

               int             int
            

            /-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-\
            | | | | | | | | | | | | | | | | |
            \-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-/
            double


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

            T A 2 Replies Last reply
            0
            • D David Crow

              Is this what you are after:

                 int             int
              

              /-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-\
              | | | | | | | | | | | | | | | | |
              \-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-/
              double


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #8

              lol, David, are you moking at newbies ? :rolleyes:


              TOXCCT >>> GEII power

              [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

              D 1 Reply Last reply
              0
              • C Chris Losinger

                int main(int argc, char* argv[])
                {
                __int32 i[2];
                i[0] = 1000;
                i[1] = 2000;

                double d = 0;
                d = \*(double\*)&i\[0\];
                
                printf("%f\\n", d);
                
                \_\_int32 \*o;
                o = (\_\_int32\*)&d;
                printf("%d %d\\n", o\[0\], o\[1\]);
                
                return 0;
                

                }

                of course, anyone doing something like this would end up in the Daily WTF, for sure.

                image processing | blogging

                A Offline
                A Offline
                Astricks
                wrote on last edited by
                #9

                I'll test this & tell you. Thanks. BRB

                1 Reply Last reply
                0
                • T toxcct

                  lol, David, are you moking at newbies ? :rolleyes:


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

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

                  Moking? Do you mean mocking? No, I was just trying to clarify what the intent was by using a visual.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                  T 1 Reply Last reply
                  0
                  • D David Crow

                    Is this what you are after:

                       int             int
                    

                    /-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-\
                    | | | | | | | | | | | | | | | | |
                    \-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-/
                    double


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                    A Offline
                    A Offline
                    Astricks
                    wrote on last edited by
                    #11

                    Yes David.

                    1 Reply Last reply
                    0
                    • D David Crow

                      Moking? Do you mean mocking? No, I was just trying to clarify what the intent was by using a visual.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #12

                      DavidCrow wrote:

                      Moking? Do you mean mocking?

                      indeed i was. but that's true also that pictures are more clear than words sometimes...


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]

                      1 Reply Last reply
                      0
                      • C Chris Losinger

                        int main(int argc, char* argv[])
                        {
                        __int32 i[2];
                        i[0] = 1000;
                        i[1] = 2000;

                        double d = 0;
                        d = \*(double\*)&i\[0\];
                        
                        printf("%f\\n", d);
                        
                        \_\_int32 \*o;
                        o = (\_\_int32\*)&d;
                        printf("%d %d\\n", o\[0\], o\[1\]);
                        
                        return 0;
                        

                        }

                        of course, anyone doing something like this would end up in the Daily WTF, for sure.

                        image processing | blogging

                        A Offline
                        A Offline
                        Astricks
                        wrote on last edited by
                        #13

                        Chris, that worked thanks. Now I'm happy I have a way. But if you can still get me a way that will never take part in WTFs that'd be excellent.

                        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