简体   繁体   English

浮点数的二进制序列化(仅限于IPC)

[英]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);

Since the sender and the receiver are identical architectures, there are no endian issues, floating point, integer, or otherwise. 由于发送方和接收方是相同的体系结构,因此没有字节序问题,浮点数,整数或其他形式。

If the architectures are different, you might have problems. 如果架构不同,则可能会有问题。 See this post or this post . 看到这个职位或这个职位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM