将 ip 地址转换为单个数字
convert ip address to a single number
我有一个包含 100 多个 IP 地址的文件(虚线 decimal.eg.169.23.43.12)。现在我需要读取所有的 ip 地址并按升序排序。为此,首先我尝试将所有 ip 地址转换为其等效整数 number.I 创建了一个 c++ 函数来转换 ip 地址,但它不适用于大 ip 地址,例如 255.250.120.100。我尝试使用 inet_aton() 和 inet_ntoa()。但是使用这两个,我无法对 ip 地址进行排序。所以,请给我一个将 ip 地址转换为可以排序的形式的想法。下面是一些代码,我尝试使用这些代码对 ip 地址进行排序,但没有用。
构造sockaddr_in羚羊[2];
字符 *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;
将地址转换为无符号整数。代码可能如下所示:
// 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];
将所有标识符插入一些 vector/array 并对其进行排序。
您可以使用结构 sokaddr_in 的自定义比较器来执行此操作。下面的片段解释了我的意思。这种方法的优点是您可以为 IPv6 自定义比较器并包括端口号。和其他东西,如果 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;
}
希望对您有所帮助。
第三个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;
}
使用 std::istringstream
.
将字符串格式的 IPv4 地址转换为无符号整数的解决方案
#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
我有一个包含 100 多个 IP 地址的文件(虚线 decimal.eg.169.23.43.12)。现在我需要读取所有的 ip 地址并按升序排序。为此,首先我尝试将所有 ip 地址转换为其等效整数 number.I 创建了一个 c++ 函数来转换 ip 地址,但它不适用于大 ip 地址,例如 255.250.120.100。我尝试使用 inet_aton() 和 inet_ntoa()。但是使用这两个,我无法对 ip 地址进行排序。所以,请给我一个将 ip 地址转换为可以排序的形式的想法。下面是一些代码,我尝试使用这些代码对 ip 地址进行排序,但没有用。
构造sockaddr_in羚羊[2]; 字符 *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;
将地址转换为无符号整数。代码可能如下所示:
// 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];
将所有标识符插入一些 vector/array 并对其进行排序。
您可以使用结构 sokaddr_in 的自定义比较器来执行此操作。下面的片段解释了我的意思。这种方法的优点是您可以为 IPv6 自定义比较器并包括端口号。和其他东西,如果 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;
}
希望对您有所帮助。
第三个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;
}
使用 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