簡體   English   中英

將IP地址從int更改為字符串的問題

[英]Issues changing IP Address from int to string

我正在嘗試將IP地址(當前為IPv4,但可識別IPv6)從整數數組格式轉換為格式化字符串。 我正在使用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也得到了這個錯誤:

格式'%s'期望的參數類型為'char *',但是參數2的類型為'int'錯誤!**

為什么會發生這種情況,我該如何解決?

m_ip[0]指向第一個元素的位置。 如果要打印字符串,請嘗試使用m_ip即數組名)或&m_ip[0] ,它們基本上會傳遞第一個元素的地址,而第一個元素的地址可能會被解釋為pointer ,從而導致分段錯誤。 使用m_ip可以打印(但仍然只有0.0.0.10 )。

程序中的另一點是在strstr操作之后應將mmNULL進行比較。

對於網絡地址操作,我建議您看一下這個問題

我對您的程序存有疑問。 您要轉換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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM