正在将IP地址字节转换为整数
Converting IP Address bytes to integer
例如:
1.要转换32位IP地址(IPv4):
unsigned char ByteAddress[4]; unsigned int* IntegerAddress; IntegerAddress = reinterpret_cast<unsigned int*> &ByteAddress ;
然后我可以使用IntegerAddress来比较ip地址。
2.要转换128位IP地址(IPv6):
unsigned char ByteAddress[16]; uint64_t* LongAddress; LongAddress = reinterpret_cast<uint64_t*> &ByteAddress
然后我可以使用LongAddress[0]和LongAddress[1]来比较ip地址。
它是否比使用移位运算符更可取(因为它更快)?这是良好的编程实践吗?它适用于所有平台(尤其是unix和windows64)和编译器(C++、VS2010)
我认为,你可以为使用联合
union Int32And4Bytes {
int32_t IntegerValue;
unsigned char ByteArray[4];
};
它应该正确对齐,IIRC。
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何在C++中将整数转换为其数字数组
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何使用C++将字符串中的字符转换为整数变量
- 如何将整数字符串转换为整数的二维向量?
- 如果整数与指针大小相同,则重新解释将整数转换为指针双射是否具有双射作用?
- 使用 "stringstream " 将字符串转换为整数
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 如何检测 std::vector::emplace_back 上的隐式转换损失整数精度
- 如何防止双精度值到整数的隐式转换
- 为什么此指针值不能转换为整数的规则是什么?
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 在 C++ 中将整数数组转换为位集表示形式的最佳方法?
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 将字符串(可以是十进制字符串或十六进制字符串)转换为整数C++
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 如何使用 STL 算法将整数向量转换为字符串向量?
- 无法将"整数 (*)[m]"转换为"整数 (*)[100000]
- 静态强制转换允许转换对象指针,但不允许转换整数