-
byte problem
ok say in my program i make a double variable for example. Somewhere in memory is my double, and it consists fundamentally of bytes in RAM. I would like to know how to get an array of bytes consisting of hte bytes that make up my double in RAM.
thanks for any help :)
-
Code:
double d = 4.2;
unsigned char *pByte = (unsigned char*)(&d);
for(int i=0; i<sizeof(double)/sizeof(unsigned char); i++)
cout << pByte[i];
...
pByte is your needed;
-
Alternately (Is there any spell error with this word?):
Code:
union Dbl_Byte
{
double d;
unsigned char c[sizeof(double)];
};
double YourDouble;
Dbl_Byte u;
u.d = YourDouble;
...
then, u.c is your needed.
-
are you looking for a bitfield reprsentation of your double?
int num = 7;
char bit[9];
bit[0] = 0;
bit[1] = 0;
bit[2] = 0;
bit[3] = 0;
bit[4] = 0;
bit[5] = 1;
bit[6] = 1;
bit[7] = 1;
bit[8] = '\0';
cout << "bitfield of 7 in most significant bit to least significant bit format = " << bit;
-
thanks hotman that union is what i need, but shouldnt hte size of the char string be sizeof(double) + 1 to account for the terminating char?
-
It is an array of char, not a string. It need not NULL char to make end. And, maybe one of it's bytes is NULL.