将IP地址转换为单个数字
convert ip address to a single number
i有一个文件,其中包含100多个IP地址(点缀Depimal.eg。169.23.43.12)。现在,我需要阅读所有IP地址并按顺序排序。为此,首先,我尝试将所有IP地址转换为其等效整数编号。我创建了一个C 功能来转换IP地址,但它不适用于大型IP地址,例如255.250.120.100。我尝试使用inet_aton()和inet_ntoa()。但是使用这两个,我无法对IP地址进行排序。因此,请给我一个想法,将IP地址转换为可以分类的表单。以下是我尝试对IP地址进行排序但无效的一些代码。
struct sockaddr_in羚羊[2]; char *some_addr;
inet_aton("60.0.0.4", &antelope[0].sin_addr); // store IP in antelope
inet_aton("10.0.0.2", &antelope[1].sin_addr); // store IP in antelope
std::sort(antelope,antelope+2);
cout<<inet_ntoa(antelope[0].sin_addr)<<endl;
cout<<inet_ntoa(antelope[1].sin_addr)<<endl;
您可以使用struct sokaddr_in的自定义比较器来执行此操作。下面的片段解释了我的意思。这种方法的优点是您可以自定义IPv6的比较器并包括端口NOS。如果IP地址相同,则其他内容。
#include <iostream>
#include <algorithm>
#include <arpa/inet.h>
struct CompareSockAddr_in
{
bool operator ()(struct sockaddr_in ip1,struct sockaddr_in ip2){
// use return ip1.sin_addr.s_addr < ip2.sin_addr.s_addr; for ascending order
return ip1.sin_addr.s_addr > ip2.sin_addr.s_addr;
}
};
int main()
{
struct sockaddr_in antelope[2];
inet_pton(AF_INET, "10.0.0.2", &(antelope[0].sin_addr));
inet_pton(AF_INET, "60.0.0.4", &(antelope[1].sin_addr));
std::cout<<inet_ntoa(antelope[0].sin_addr)<<std::endl;
std::cout<<inet_ntoa(antelope[1].sin_addr)<<std::endl;
std::sort(antelope,antelope+2,CompareSockAddr_in());
std::cout<<"Sorted List...n";
std::cout<<inet_ntoa(antelope[0].sin_addr)<<std::endl;
std::cout<<inet_ntoa(antelope[1].sin_addr)<<std::endl;
return 0;
}
希望这会有所帮助。
使用std::istringstream
。
#include <sstream>
uint32_t convert( const std::string& ipv4Str )
{
std::istringstream iss( ipv4Str );
uint32_t ipv4 = 0;
for( uint32_t i = 0; i < 4; ++i ) {
uint32_t part;
iss >> part;
if ( iss.fail() || part > 255 ) {
throw std::runtime_error( "Invalid IP address - Expected [0, 255]" );
}
// LSHIFT and OR all parts together with the first part as the MSB
ipv4 |= part << ( 8 * ( 3 - i ) );
// Check for delimiter except on last iteration
if ( i != 3 ) {
char delimiter;
iss >> delimiter;
if ( iss.fail() || delimiter != '.' ) {
throw std::runtime_error( "Invalid IP address - Expected '.' delimiter" );
}
}
}
return ipv4;
}
示例结果
"0.0.0.5" => 5
"192.168.0.5" => 3232235525
"255.250.120.100" => 4294604900
"255.255.255.255" => 4294967295
将地址转换为无符号整数。代码看起来像这样:
// If ip is 132.152.25.103, then unsigned int IP = {132, 152, 25, 103};
unsigned int identifier = 0;
identifier = ((IP[0]*255 + IP[1])*255 + IP[2])*255 + IP[3];
将所有标识符插入某些向量/数组并对其进行排序。
第三个inet_pton参数是指向IN_ADDR结构的指针。
成功进行INET_PTON调用后,IN_ADDR结构将填充地址信息。结构的S_ADDR字段包含网络字节顺序(反向顺序)中的IP地址。NTOHL函数将将地址从网络字节订单转换为托管字节订单。
Example :
#include <arpa/inet.h>
uint32_t NodeIpAddress::getIPv4AddressInteger(std::string IPv4Address) {
int result;
uint32_t IPv4Identifier = 0;
struct in_addr addr;
// store this IP address in sa:
result = inet_pton(AF_INET, IPv4Address.c_str(), &(addr));
if (result == -1) {
gpLogFile->Write(LOGPREFIX, LogFile::LOGLEVEL_ERROR, _T("Failed to convert IP %hs to IPv4 Address. Due to invalid family of %d. WSA Error of %d"), IPv4Address.c_str(), AF_INET, result);
}
else if (result == 0) {
gpLogFile->Write(LOGPREFIX, LogFile::LOGLEVEL_ERROR, _T("Failed to convert IP %hs to IPv4"), IPv4Address.c_str());
}
else {
IPv4Identifier = ntohl(*((uint32_t *)&(addr)));
}
return IPv4Identifier;
}
相关文章:
- 如何将较大数字中从0到9的单个数字放入只包含数字的数组中
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- C/C++ - 用于按顺序打印数字的 sem_t 类型的单个信号量
- 如何使用位操作在单个整数中编码和解码两个数字
- 如何以相反顺序C 打印整数的单个数字
- 为什么SNPRINTF始终比OSTRINGTREAM快2倍,以打印单个数字
- put_time具有转换说明符,该转换指定符,该指定符在一个月的单个数字上输出没有前面字符的单位天数
- 我想制作一个代码,将C 中的单个数字值分开.例如:12345 = 1 2 3 4 5
- 是否可以使整数仅收到一个单个数字而不是两个接收输入
- 使用单个数字表示多个变量
- 如何与C 中INT的单个数字一起工作
- 如何使用字符串数组来分解一个数字的单个数字
- 将IP地址转换为单个数字
- 将两个数字替换为给定数组中介于两个之间的单个数字
- 我如何将2位数字分为单个数字
- 来自单个数字输入的Arduino读取脉冲宽度频率和占空比
- 读取整数的单个数字
- 我需要让一个数字输入的用户输出单个数字使用c++
- 打开c++ TXT文件并将单个数字读入int数组
- 单个数字作为xy坐标