c - 接收浮点数的序列化(花车,双打) [英] C - Serialization of the floating point numbers (floats, doubles)

查看:137
本文介绍了c - 接收浮点数的序列化(花车,双打)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何浮点数转换成一个字节序列,以便它可以在一个文件中被持久?这种算法必须快速和高度便携。它还必须允许相反的操作,反序列化。如果只需要非常小的超额每个值位(持久空间)这将是很好。

How to convert a floating point number into a sequence of bytes so that it can be persisted in a file? Such algorithm must be fast and highly portable. It must allow also the opposite operation, deserialization. It would be nice if only very tiny excess of bits per value (persistent space) is required.

推荐答案

假设你正在采用主流的编译器,漂浮在C和C点值++服从IEEE标准,写成二进制形式的文件时,可以在任何恢复其他平台,但前提是你写的,并使用相同的字节字节顺序读取。所以我的建议是:挑选择的字节序,写读前后,检查如果字节顺序是一样的,在目前的平台;如果没有,只是交换字节。

Assuming you're using mainstream compilers, floating point values in C and C++ obey the IEEE standard and when written in binary form to a file can be recovered in any other platform, provided that you write and read using the same byte endianess. So my suggestion is: pick an endianess of choice, and before writing or after reading, check if that endianess is the same as in the current platform; if not, just swap the bytes.

这篇关于c - 接收浮点数的序列化(花车,双打)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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