Can double be used to store and safely retrieve 128 bit IPv6?

こ雲淡風輕ζ 提交于 2019-12-12 05:04:44

问题


As we know double can store number from ±4.94065645841246544e-324 to ±1.79769313486231570e+308. Can we store 128 bits value like IPv6 in double and retrieve as it is? For example, to store IPv4 we can use a 32 bit integer and store them as 8 bits partition. And we can retrieve them by using bit masking.

There is no standard container to store 128 bit in any language AFAIK. I know double can store 64 bits safely without any precision error but is there any hack?


回答1:


Technically you could, if sizeof(double) * CHAR_BITS ≥ 128 (it's 64bits on my machine), but why would you do that? Instead of reinventing the wheel use sockaddr_in6, or, if you must, an array of uint8_t or a std::bitset.

Related answer: Efficient way to store IPv4/IPv6 addresses




回答2:


I would be reluctant to use a double because its size is unspecified.

If you need a 128 bit integer, I would recommend to use non-standard types as explained in this question.



来源:https://stackoverflow.com/questions/40050286/can-double-be-used-to-store-and-safely-retrieve-128-bit-ipv6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!