[英]Binary serialization of floating point numbers (restricted to IPC)
I have two processes running on the same device (no VMs involved), communicating over a binary IPC protocol. 我有两个进程在同一设备上运行(不涉及虚拟机),它们通过二进制IPC协议进行通信。 Since this guarantees that the the representation of the number is the same for the sender and the receiver, can I safely assume that the following serialization will work on any device that supports floating point numbers? 由于这保证了发送方和接收方的数字表示形式相同,因此我可以安全地假定以下序列化可在任何支持浮点数的设备上使用吗?
void store_double(uint8_t *buf, double d)
{
memcpy(buf, &d, sizeof(double));
}
double load_double(uint8_t const *buf)
{
double d;
memcpy(&d, buf, sizeof(double));
return d;
}
double orig = 123.456;
uint8_t serialized[sizeof(double)];
store_double(serialized, orig);
// send serialized bytes to the receiver
// receive serialized bytes from the sender
double copy = load_double(serialized);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.