序列化double和float用C [英] Serialize double and float with C

查看:839
本文介绍了序列化double和float用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以序列双打和用C浮?

How can I serialize doubles and floats in C?

我有以下的code串行化短裤,整数和字符。

I have the following code for serializing shorts, ints, and chars.

unsigned char * serialize_char(unsigned char *buffer, char value)
{
    buffer[0] = value;
    return buffer + 1;
}

unsigned char * serialize_int(unsigned char *buffer, int value)
{
    buffer[0] = value >> 24;
    buffer[1] = value >> 16;
    buffer[2] = value >> 8;
    buffer[3] = value;
    return buffer + 4;
}

unsigned char * serialize_short(unsigned char *buffer, short value)
{
    buffer[0] = value >> 8;
    buffer[1] = value;
    return buffer + 2;
}

编辑:

我发现从

编辑2:

串行化的目的是将数据发送到UDP套接字并保证它可以在其他的机器上进行反序列化,即使字节顺序是不同的。是否有任何其他的最佳实践来执行这个功能给我有序列化整数,双打,花车,和char *?

The purpose of serializing is to send data to a UDP socket and guarantee that it can be deserialized on the other machine even if the endianness is different. Are there any other "best practices" to perform this functionality given that I have to serialize ints, doubles, floats, and char*?

推荐答案

按照你的更新,你提到的数据是使用UDP进行传输,并要求最佳实践。我会强烈建议将数据作为文本,甚至与索姆加标记(XML)。跨越传输线调试尾数相关型号的错误是每个人的浪费时间

Following your update, you mention the data is to be transmitted using UDP and ask for best practices. I would highly recommend sending the data as text, perhaps even with som markup added (XML). Debugging endian-releated errors across a transmission-line is a waste of everybodys time

只是我的2对最佳做法你的问题的一部分美分

Just my 2 cents on the "best practices" part of your question

这篇关于序列化double和float用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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