I've got two mbeds, each with an xbee. I'm trying to send double (8 byte) values as 8 characters with mbed master, receieve them with mbed client and put them together again.
/*
* Send double as 8 characters
*/
void xbee_send_double(double number){
long long * ptr = (long long *) (& number); //pointer to number address to fool compiler
pc.printf("length of long long: %d\n", sizeof(long long));
pc.printf("num = %f\n", number);
for (int i = 0; i < sizeof(long long); ++i){
xbee.putc((*ptr >> (8 * i)) & 0xFF);
pc.printf("%d\n", (*ptr >> (8 * i)) & 0xFF);
}
}
int main() {
xbee_send_double(12.3456);
}
This prints to the pc:
length of long long: 8
num = 12.345600
197
254
178
123
242
176
40
64
--
On the other mbed, I've got this code:
/*
* Receive double as 8 characters
*/
double xbee_get_double(){
long long data = 0;
for (int i = 0; i < sizeof(double); ++i){
char temp = xbee.getc();
pc.printf("temp = \'%d\'\n", temp);
data = data | (temp << (8*i));
pc.printf("data = \'%lld\'\n", data);
}
double * ptr = (double *) (&data); //pointer to data address
return *ptr;
}
int main() {
while (1) {
if (xbee.readable()) {
double data = xbee_get_double();
pc.printf("float = %f\n", data);
pc.printf("float = %d\n", data);
pc.printf("float = %e\n", data);
}
}
}
This prints the following to the terminal:
temp = '197'
data = '197'
temp = '254'
data = '65221'
temp = '178'
data = '11730629'
temp = '123'
data = '2075328197'
temp = '242'
data = '2075328197'
temp = '176'
data = '2075328197'
temp = '40'
data = '2075328197'
temp = '64'
data = '2075328197'
float = 0.000000
float = 2075328197
float = 1.025348e-314
As you can see, the 'temp' characters transfer correctly, but data stays at the same value for the 5th-8th character (in red).
Since this method works for ints (4 bytes = 32 bits), I'm wondering if it's a problem using 8 bytes = 64 bits and bitwise operations?
I've got two mbeds, each with an xbee. I'm trying to send double (8 byte) values as 8 characters with mbed master, receieve them with mbed client and put them together again.
This prints to the pc:
--
On the other mbed, I've got this code:
This prints the following to the terminal:
temp = '197' data = '197' temp = '254' data = '65221' temp = '178' data = '11730629' temp = '123' data = '2075328197' temp = '242' data = '2075328197' temp = '176' data = '2075328197' temp = '40' data = '2075328197' temp = '64' data = '2075328197' float = 0.000000 float = 2075328197 float = 1.025348e-314
As you can see, the 'temp' characters transfer correctly, but data stays at the same value for the 5th-8th character (in red).
Since this method works for ints (4 bytes = 32 bits), I'm wondering if it's a problem using 8 bytes = 64 bits and bitwise operations?