Steph said:
i'm using visual studio 2005, C++
I have these variables
byte buffer1[100];
long lng1;
Bool bl1;
date dt1;
I want to concatenate lng1, bl1 and dt1 values into buffer1.
How can I convert a variable to byte
I think that bool is 1 byte.
how can I transfer 2 bytes for this field instead of only 1 to buffer1
thanks
this is one of those "be sure you know what you're doing!" things.
btw, what's a "date"? I used time_t, since I wasn't sure. I assumed "byte"
was "unsigned char". The code below is mostly C style, but, "bool" is a C++
keyword, so it's a cpp file.
#include <time.h>
#include <memory.h>
#include <stdio.h>
struct MyConcatenator
{
long lng1;
bool bl1;
time_t tm1;
};
int main(int argc, char* argv[])
{
unsigned char buffer1[100];
struct MyConcatenator mc;
struct MyConcatenator mc2;
mc.lng1 = 10000;
mc.bl1 = true;
time(&mc.tm1);
// copy to buffer
memcpy(buffer1,&mc,sizeof(mc));
// copy from buffer
memcpy(&mc2,buffer1,sizeof(mc2));
printf("All safe and sound: %ld\n",mc2.lng1);
return 0;
}
--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]