5

例如,我有一个 IP 地址char *ip = "192.168.1.13",需要将其转换为"13.1.168.192" C 中是否存在适当的可能性,还是我必须自己通过简单地制作令牌并将它们重新组合在一起来完成?

4

5 回答 5

10

您始终可以使用inet_pton将其转换为二进制形式,使其易于反转,然后使用inet_ntop.


请记住,IPv4 地址只是一个 32 位无符号整数。交换字节非常容易。

像这样的东西:

const char ip[] = "192.168.0.1";
char reversed_ip[INET_ADDRSTRLEN];

in_addr_t addr;

/* Get the textual address into binary format */
inet_pton(AF_INET, ip, &addr);

/* Reverse the bytes in the binary address */
addr =
    ((addr & 0xff000000) >> 24) |
    ((addr & 0x00ff0000) >>  8) |
    ((addr & 0x0000ff00) <<  8) |
    ((addr & 0x000000ff) << 24);

/* And lastly get a textual representation back again */
inet_ntop(AF_INET, &addr, reversed_ip, sizeof(reversed_ip));

在此代码之后,变量reversed_ip包含作为字符串的受尊敬的地址。

于 2013-05-04T10:29:48.133 回答
4
int a,b,c,d;
char *ip = "192.168.1.13"
char ip2[16];
sscanf(ip,"%d.%d.%d.%d",&a,&b,&c,&d);
sprintf(ip2, "%d.%d.%d.%d", d, c, b, a);
于 2013-05-04T10:47:07.737 回答
2
#include <stdio.h>
#include <string.h>

void swap(char *a, char *b){
    char wk = *a;
    *a = *b;
    *b = wk;
}

void strpartrev(char *s, int len){
    int i,j;
    for(i=0,j=len-1; i<len/2 ;++i,--j)
        swap(s + i, s + j);
}

char *strwordrev(char *str, char delimiter){
    //str change destructively
    int sp = -1, wordlen=0;
    char stack[16], *p=str;

    while(*p){
        if(*p == delimiter){
            strpartrev(stack + sp - wordlen + 1, wordlen);
            wordlen = 0;
        } else {
            ++wordlen;
        }
        stack[++sp] = *p++;
    }
    strpartrev(stack + sp - wordlen + 1 , wordlen);
    p = str;
    do{
        *p++ = stack[sp--];
    }while(sp>=0);

    return str;
}

char *strWordRev(char *str, char delimiter){
    //str change destructively
    char *head, *tail;
    int len = strlen(str);

    head = str;
    while(tail = strchr(head, delimiter)){
        strpartrev(head, tail - head);
        head = tail + 1;
    }
    strpartrev(head, str + len - head);
    strpartrev(str, len);

    return str;
}

int main(void){
    char *ip = "192.168.1.13";
    char rip[16];

    strcpy(rip, ip);
    printf("%s\n", strWordRev(rip, '.'));//13.1.168.192

    return 0;
}
于 2013-05-04T11:51:26.110 回答
1

您可以将字符串转换为addrinfowith getaddrinfo。指定AI_NUMERICHOST以防止 DNS 查找。

addrinfo hint;
memset(&hint, 0, sizeof(addrinfo));
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
hint.ai_family = AF_INET;
hint.ai_flags = AI_NUMERICHOST; // prevents dns lookup

addrinfo *result = nullptr;
if (getaddrinfo(name, nullptr, &hint, &result))
    return false;

uint32_t ip;
memcpy(&ip, &result, sizeof(uint32_t));

uint32_t ipreversed = htonl(ip);

freeaddrinfo(result);
于 2013-05-04T10:36:37.050 回答
1

将 ip 拆分为一个数组,并使用代码反转该数组。

这是我在这里找到的一段简洁的数组反转代码:http: //www.programmingsimplified.com/c-program-reverse-array

#include <stdio.h>

int main()
{
   int n, c, d, a[100], b[100];

   printf("Enter the number of elements in array\n");
   scanf("%d", &n);

   printf("Enter the array elements\n");

   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);

   /*
    * Copying elements into array b starting from end of array a
    */

   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];

   /*
    * Copying reversed array into original.
    * Here we are modifying original array, this is optional.
    */

   for (c = 0; c < n; c++)
      a[c] = b[c];

   printf("Reverse array is\n");

   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);

   return 0;
}
于 2013-05-04T10:30:34.010 回答