C - Serialization of the floating point numbers (floats, doubles)

后端 未结 10 1064
攒了一身酷
攒了一身酷 2020-11-28 11:18

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 th

10条回答
  •  青春惊慌失措
    2020-11-28 11:56

    Converting to an ascii representation would be the simplest, but if you need to deal with a colossal number of floats, then of course you should go binary. But this can be a tricky issue if you care about portability. Floating point numbers are represented differently in different machines.

    If you don't want to use a canned library, then your float-binary serializer/deserializer will simply have to have "a contract" on where each bit lands and what it represents.

    Here's a fun website to help with that: link.

提交回复
热议问题