如何将字符串转换为IP地址,反之亦然

问题描述 投票:65回答:9

如何转换字符串ipAddress(struct in_addr),反之亦然?以及如何打开unsigned long ipAddress?谢谢

c++ string struct ip-address in-addr
9个回答
118
投票

如果你需要其他方法,请使用inet_ntop()inet_pton()。不要使用inet_ntoa(), inet_aton()和类似的,因为它们已被弃用且不支持ipv6。

这是一个很好的guide,有很多例子。

// IPv4 demo of inet_ntop() and inet_pton()

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));

// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);

printf("%s\n", str); // prints "192.0.2.33"

5
投票

我能够将字符串转换为DWORD并返回此代码:

char strAddr[] = "127.0.0.1"
DWORD ip = inet_addr(strAddr); // ip contains 16777343 [0x0100007f in hex]

struct in_addr paddr;
paddr.S_un.S_addr = ip;

char *strAdd2 = inet_ntoa(paddr); // strAdd2 contains the same string as strAdd

我正在使用旧MFC代码的维护项目,因此转换已弃用的函数调用不适用。


3
投票

inet_ntoa()in_addr转换为字符串:

inet_ntoa函数将(Ipv4)Internet网络地址转换为Internet标准点分十进制格式的ASCII字符串。

inet_addr()做了相反的工作

inet_addr函数将包含IPv4点分十进制地址的字符串转换为IN_ADDR结构的正确地址

PS这是第一个搜索“in_addr to string”的结果!


3
投票

我不确定我是否理解这个问题。

无论如何,你在寻找这个:

std::string ip ="192.168.1.54";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

Output:

192  168  1  54

2
投票

要将字符串转换为in-addr:

in_addr maskAddr;
inet_aton(netMaskStr, &maskAddr);

要将in_addr转换为字符串:

char saddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &inaddr, saddr, INET_ADDRSTRLEN);

1
投票

此示例显示如何从字符串转换为ip,反之亦然:

struct sockaddr_in sa;
char ip_saver[INET_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET, "192.0.1.10", &(sa.sin_addr));

// now get it back 
sprintf(ip_saver, "%s", sa.sin_addr));

// prints "192.0.2.10"
printf("%s\n", ip_saver); 

0
投票

第三个inet_pton参数是指向in_addr结构的指针。在成功调用inet_pton之后,in_addr结构将填充地址信息。结构的S_addr字段包含网络字节顺序的IP地址(反向顺序)。


0
投票

这里是易于使用的,线程安全的c ++函数,用于将uint32_t native-endian转换为string,将string转换为native-endian uint32_t:

#include <arpa/inet.h> // inet_ntop & inet_pton
#include <string.h> // strerror_r
#include <arpa/inet.h> // ntohl & htonl
using namespace std; // im lazy

string ipv4_int_to_string(uint32_t in, bool *const success = nullptr)
{
    string ret(INET_ADDRSTRLEN, '\0');
    in = htonl(in);
    const bool _success = (NULL != inet_ntop(AF_INET, &in, &ret[0], ret.size()));
    if (success)
    {
        *success = _success;
    }
    if (_success)
    {
        ret.pop_back(); // remove null-terminator required by inet_ntop
    }
    else if (!success)
    {
        char buf[200] = {0};
        strerror_r(errno, buf, sizeof(buf));
        throw std::runtime_error(string("error converting ipv4 int to string ") + to_string(errno) + string(": ") + string(buf));
    }
    return ret;
}
// return is native-endian
// when an error occurs: if success ptr is given, it's set to false, otherwise a std::runtime_error is thrown.
uint32_t ipv4_string_to_int(const string &in, bool *const success = nullptr)
{
    uint32_t ret;
    const bool _success = (1 == inet_pton(AF_INET, in.c_str(), &ret));
    ret = ntohl(ret);
    if (success)
    {
        *success = _success;
    }
    else if (!_success)
    {
        char buf[200] = {0};
        strerror_r(errno, buf, sizeof(buf));
        throw std::runtime_error(string("error converting ipv4 string to int ") + to_string(errno) + string(": ") + string(buf));
    }
    return ret;
}

公平警告,在写作时,他们没有经过测试。但是当我来到这个线程时,这些功能正是我所寻找的。


0
投票

字符串IP的十六进制IP地址

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    uint32_t ip = 0x0AA40001;
    string ip_str="";
    int temp = 0;
    for (int i = 0; i < 8; i++){
        if (i % 2 == 0)
        {
            temp += ip & 15;
            ip = ip >> 4;
        }
        else
        {
            stringstream ss;
            temp += (ip & 15) * 16;
            ip = ip >> 4;
            ss << temp;
            ip_str = ss.str()+"." + ip_str;
            temp = 0;
        }
    }
    ip_str.pop_back();
    cout << ip_str;
}

输出:10.164.0.1

© www.soinside.com 2019 - 2024. All rights reserved.