可以使用double来存储和安全地检索128位IPv6吗? [英] Can double be used to store and safely retrieve 128 bit IPv6?

查看:82
本文介绍了可以使用double来存储和安全地检索128位IPv6吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道 double 可以存储从±4.9406564584124654654e-324 ±的数字1.79769313486231570e + 308 。我们可以在 double 中存储IPv6之类的128位值并按原样检索吗?例如,要存储IPv4,我们可以使用32位整数并将其存储为8位分区。我们可以使用位掩码来检索它们。

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.

没有标准容器可以存储任何语言的AFAIK 128位。我知道 double 可以安全地存储64位而没有任何精度错误,但是有没有黑客之处?

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?

推荐答案

从技术上讲,如果 sizeof(double)* CHAR_BITS ≥128(在我的计算机上为64位),则可以,但是为什么要这样做?不用重新发明轮子,而是使用 sockaddr_in6 ,或者,如果需要的话,使用 uint8_t 的数组或 std :: bitset

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.

相关答案:有效方式存储IPv4 / IPv6地址

这篇关于可以使用double来存储和安全地检索128位IPv6吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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