在双精度和字节数组之间进行转换,以便通过 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.
Basically, I need to turn each double into an array of eight raw bytes, then take that raw data and reconstruct the double again.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所做的称为<a href="http://en.wikipedia.org/wiki/Type_punning" rel="noreferrer">类型双关。
使用联合:
或使用
reinterpret_cast
:What you're doing is called type punning.
Use a union:
Or use
reinterpret_cast
:这是一种相当不安全的方法:
或者您可以使用reinterpret_cast:
Here's a rather unsafe way to do it:
Or you could use a reinterpret_cast:
通常,双精度数已经是八个字节。请通过比较 sizeof(double) 和 sizeof(char) 在您的操作系统上验证这一点。 C++ 不声明 byte ,通常它意味着 char
如果确实如此。
现在字节就是您需要发送到 ZigBee 的内容
Typically a double is already eight bytes. Please verify this on your operating system by comparing sizeof(double) and sizeof(char). C++ doesn't declare a byte , usually it means char
If it is indeed true.
Now bytes is what you need to send to ZigBee