将 IP 地址从 int 更改为 string 的问题

Issues changing IP Address from int to string

我正在尝试将 IP 地址(当前为 IPv4,但可识别 IPv6)从 int 数组格式转换为格式化字符串。我正在研究 Ubuntu Linux.

我区分IPv4和IPv6地址,然后尝试将int数组转换成字符串

这是我正在使用的代码:

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>

int main(void)
{
    int i;
    unsigned int val32;
    int ma_ip[4];
    char m_ip[4];
    char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
    int no = 0;
    char *token;
    char s3[] = "10.1.35.1";
    /* just example. test only one ip address.. */
    for(i = 0; i < 1; i++)
    {
        char *mm = strstr(s3, ":");
        if( *mm != NULL)
        {
             token = strtok(s3, ":");
             while(token != NULL)
             {
                 token = strtok(NULL, ":");
                 no++;
             }
             if(no >= 2 && no <= 7)
                 printf("\nthis is ipv6\n");
             else
                 printf("\nwrong ipv6\n");
         }
         else
         {
             token = strtok(s3, ".");
             while(token != NULL)
             {
                 token = strtok(NULL, ".");
                 no++;
             }
             if(no == 4)
             {
                 printf("\nthis is ipv4.\n");
                 val32 = inet_addr(s3)
                 ma_ip[i] = val32;
             }
             else
                 printf("\nwrong ipv4.\n")
         }
         inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
         printf("\nipaddr = %s\n", ipaddr);
         strcpy(&m_ip[0], ipaddr);
         printf("\nafter strcpy = %s\n", m_ip[0]);
    }
}

这个输出是错误的:

ipaddr = 0.0.0.10

实际上应该是:

ipaddr = 10.1.35.1

我在 strcpy 之后的最后一个 printf 中也遇到了这个错误:

format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ error !**

为什么会发生这种情况,我该如何解决?

m_ip[0] 指向第一个元素的位置。如果你想打印字符串,请尝试使用 m_ip 即数组名称或 &m_ip[0] ,它基本上传递第一个元素的地址,可以解释为 pointer 并且基本上导致分段故障。使用 m_ip 它打印(但仍然只有 0.0.0.10)。

你的程序还有一点就是你应该在strstr操作后比较mmNULL

关于网络地址操作,我建议你看看this question

我对你的程序有一定的疑虑。 你在转换 ip 字符串吗

 char s3[] = "10.1.35.1";

到一个整数,然后再返回到字符串??

我想这就是你需要的

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"

关于strcpy之后的最后一个printf,应该是

printf("\nafter strcpy = %s\n", m_ip);

这是一个可行的版本。您的代码的问题是,strtok() 正在修剪字符串 's3'。到 n == 4 时,字符串只剩下“10”。

 #include <stdio.h>
 #include <string.h>
 #include <arpa/inet.h>
 #include <malloc.h>

int main(void)
{
    int i;
    unsigned int val32;
    int ma_ip[4];
    char m_ip[4];
    char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
    int no = 0;
    char *token;
    char s3[] = "10.1.35.1";
    char *s4 = malloc(strlen(s3)+1);   
    strcpy(s4, s3);               //You can save the original string.
    /* just example. test only one ip address.. */
    for(i = 0; i < 1; i++)
    {
        char *mm = strstr(s3, ":");
        if( mm != NULL)
        {
             token = strtok(s3, ":");
             while(token != NULL)
             {
                 token = strtok(NULL, ":");
                 no++;
             }
             if(no >= 2 && no <= 7)
                 printf("\nthis is ipv6\n");
             else
                 printf("\nwrong ipv6\n");
         }
         else
         {
             token = strtok(s3, ".");
             while(token != NULL)
             {
                 token = strtok(NULL, ".");
                 no++;
             }
             if(no == 4)
             {
                 printf("\nthis is ipv4.\n");
                 val32 = inet_addr(s4);  //use the intact string s4, instead of s3.
                 ma_ip[i] = val32;
             }
             else
                 printf("\nwrong ipv4.\n");
         }
         inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
         printf("\nipaddr = %s\n", ipaddr);
         strcpy(&m_ip[0], ipaddr);
         printf("\nafter strcpy = %s\n", m_ip);
    }
}