Converting double to BYTE array
-
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
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] -
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
-
unsigned char bArray[sizeof(double)]; double d; memcpy(bArray,&d, sizeof(double)); reverse: memcpy(&d, bArray, sizeof(double));
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] -
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] -
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] -
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] -
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
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.
-
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]