在双字节和字节数组之间转换,通过ZigBee API传输? [英] Convert between double and byte array, for transfer over ZigBee API?

查看:168
本文介绍了在双字节和字节数组之间转换,通过ZigBee API传输?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取两个双精度(GPS坐标),并通过ZigBee API将它们发送到另一个ZigBee接收器单元,但我不知道如何将双字节分解成字节数组,然后重新组合成他们的原始形式一旦他们转移。

I'm trying to take two doubles (GPS coordinates) and send them over the ZigBee API to another ZigBee receiver unit, but I don't know how to decompose the doubles into byte arrays and then re-compose them back into their original form once they are transferred.

基本上,我需要将每个double变成一个包含8个原始字节的数组,然后获取原始数据并重新构建double。

Basically, I need to turn each double into an array of eight raw bytes, then take that raw data and reconstruct the double again.

任何想法?

推荐答案

你所做的是 type punning

使用工会:

union {
  double d[2];
  char b[sizeof(double) * 2];
};

或reinterpret_cast:

Or reinterpret_cast:

char* b = reinterpret_cast<double*>(d);

这篇关于在双字节和字节数组之间转换,通过ZigBee API传输?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆