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. Converting double to BYTE array

Converting double to BYTE array

Scheduled Pinned Locked Moved C / C++ / MFC
databasemysqldata-structureshelp
9 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.
  • B Offline
    B Offline
    ba5h33r
    wrote on last edited by
    #1

    Hi All, i am intending to save double array values to blob fiels in MySql database. So i need to convert double value in to BYTE array. Can anyone help me to convert a double value (8byte) to BYTE array and vice versa. Thanks in advance Basheer

    T K N 3 Replies Last reply
    0
    • B ba5h33r

      Hi All, i am intending to save double array values to blob fiels in MySql database. So i need to convert double value in to BYTE array. Can anyone help me to convert a double value (8byte) to BYTE array and vice versa. Thanks in advance Basheer

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

      double d = /*...*/;
      char tab[8];
      tab[0] = ((d >> 0) & 0xFF);
      tab[1] = ((d >> 8) & 0xFF);
      tab[2] = ((d >> 16) & 0xFF);
      tab[3] = ((d >> 24) & 0xFF);
      tab[4] = ((d >> 32) & 0xFF);
      tab[5] = ((d >> 40) & 0xFF);
      tab[6] = ((d >> 48) & 0xFF);
      tab[7] = ((d >> 56) & 0xFF);


      TOXCCT >>> GEII power
      [toxcct][VisualCalc 2.20][VisualCalc 3.0]

      1 Reply Last reply
      0
      • B ba5h33r

        Hi All, i am intending to save double array values to blob fiels in MySql database. So i need to convert double value in to BYTE array. Can anyone help me to convert a double value (8byte) to BYTE array and vice versa. Thanks in advance Basheer

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

        unsigned char bArray[sizeof(double)]; double d; memcpy(bArray,&d, sizeof(double)); reverse: memcpy(&d, bArray, sizeof(double));

        T 1 Reply Last reply
        0
        • K kakan

          unsigned char bArray[sizeof(double)]; double d; memcpy(bArray,&d, sizeof(double)); reverse: memcpy(&d, bArray, sizeof(double));

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

          kakan wrote:

          unsigned char bArray[sizeof(double)];

          won't compile. you have to do it this way :

          unsigned char* bArray = new unsigned char[sizeof(double)];

          which is why i directly put [8] to ease the code (standard C++ tell that the size of a double is fixed to 8 bytes, so doesn't vary).


          TOXCCT >>> GEII power
          [toxcct][VisualCalc 2.20][VisualCalc 3.0]

          K N 2 Replies Last reply
          0
          • T toxcct

            kakan wrote:

            unsigned char bArray[sizeof(double)];

            won't compile. you have to do it this way :

            unsigned char* bArray = new unsigned char[sizeof(double)];

            which is why i directly put [8] to ease the code (standard C++ tell that the size of a double is fixed to 8 bytes, so doesn't vary).


            TOXCCT >>> GEII power
            [toxcct][VisualCalc 2.20][VisualCalc 3.0]

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

            Hello. You are right (of course). But the memcpy is neat, isn't it? :laugh:

            T 1 Reply Last reply
            0
            • K kakan

              Hello. You are right (of course). But the memcpy is neat, isn't it? :laugh:

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

              yes, very good idea... but as i don't use it that much, it went off my thinking... :doh: well done


              TOXCCT >>> GEII power
              [toxcct][VisualCalc 2.20][VisualCalc 3.0]

              K 1 Reply Last reply
              0
              • T toxcct

                yes, very good idea... but as i don't use it that much, it went off my thinking... :doh: well done


                TOXCCT >>> GEII power
                [toxcct][VisualCalc 2.20][VisualCalc 3.0]

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

                Thanks.:)

                1 Reply Last reply
                0
                • B ba5h33r

                  Hi All, i am intending to save double array values to blob fiels in MySql database. So i need to convert double value in to BYTE array. Can anyone help me to convert a double value (8byte) to BYTE array and vice versa. Thanks in advance Basheer

                  N Offline
                  N Offline
                  nde_plume
                  wrote on last edited by
                  #8

                  Obviously the best wat to do this is to not do it, viz: union MyConvert { double d; BYTE bytes[sizeof(double)]; }; Thus for some function storeBytes(BYTE*p, size_t size) to get a double called myVal stored do this: MyConvert mc; mc.d = myVal; storeBytes(mc.bytes, sizeof(myVal); or if you want to go nuts and save the copy: storeBytes(((MyConvert*)(&myVal))->bytes, sizeof(myVal)); Though that might not work if the alignment requirements are different between double and unions.

                  1 Reply Last reply
                  0
                  • T toxcct

                    kakan wrote:

                    unsigned char bArray[sizeof(double)];

                    won't compile. you have to do it this way :

                    unsigned char* bArray = new unsigned char[sizeof(double)];

                    which is why i directly put [8] to ease the code (standard C++ tell that the size of a double is fixed to 8 bytes, so doesn't vary).


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc 2.20][VisualCalc 3.0]

                    N Offline
                    N Offline
                    nde_plume
                    wrote on last edited by
                    #9

                    Actually you are incorrect. sizeof(double) is a compile time constant, and so it will compile. (Just to double check, I tried and it did.)

                    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