2009-11-05 72 views
-4

我以前發佈如何實現一個函數,將整數轉換爲IP地址字符串。那麼我們怎麼去反之亦然,也就是說,給定一個字符串(154.111.23.23)是一個地址,我們怎樣才能得到這個整數,而不使用inet函數。整數IP地址 - C

+0

前一個問題是在這裏:http://stackoverflow.com/questions/1680365/integer-to-ip-address-c – 2009-11-05 13:40:29

+1

你真的應該花時間想想你問之前。 – 2009-11-05 13:41:15

回答

5

將字符串掃描爲四個字節並將它們添加/移位爲一個32位整數。

1
//No checking of the input  
unsigned int c1,c2,c3,c4; 
scanf("%d.%d.%d.%d",&c1,&c2,&c3,&c4); 
unsigned long ip = (unsigned long)c4+c3*256+c2*256*256+c1*256*256*256; 
printf("The unsigned long integer is %lu\n",ip); 

編輯:對於那些怎麼有興趣在生成的代碼,GCC是足夠聰明與左移取代我的256乘法。 (在我的程序中我也叫退出):

0x80483d4 <main>:  lea 0x4(%esp),%ecx 
0x80483d8 <main+4>:  and $0xfffffff0,%esp 
0x80483db <main+7>:  pushl -0x4(%ecx) 
0x80483de <main+10>:  push %ebp 
0x80483df <main+11>:  mov %esp,%ebp 
0x80483e1 <main+13>:  push %ecx 
0x80483e2 <main+14>:  sub $0x34,%esp 
0x80483e5 <main+17>:  lea -0x14(%ebp),%eax 
0x80483e8 <main+20>:  mov %eax,0x10(%esp) 
0x80483ec <main+24>:  lea -0x10(%ebp),%eax 
0x80483ef <main+27>:  mov %eax,0xc(%esp) 
0x80483f3 <main+31>:  lea -0xc(%ebp),%eax 
0x80483f6 <main+34>:  mov %eax,0x8(%esp) 
0x80483fa <main+38>:  lea -0x8(%ebp),%eax 
0x80483fd <main+41>:  mov %eax,0x4(%esp) 
0x8048401 <main+45>:  movl $0x8048520,(%esp) 
0x8048408 <main+52>:  call 0x8048320 <[email protected]> 
0x804840d <main+57>:  mov -0x8(%ebp),%eax 
0x8048410 <main+60>:  mov %eax,%edx 
0x8048412 <main+62>:  shl $0x8,%edx 
    0x8048415 <main+65>:  mov -0xc(%ebp),%eax 
0x8048418 <main+68>:  lea (%edx,%eax,1),%eax 
0x804841b <main+71>:  mov %eax,%edx 
0x804841d <main+73>:  shl $0x8,%edx 
0x8048420 <main+76>:  mov -0x10(%ebp),%eax 
0x8048423 <main+79>:  lea (%edx,%eax,1),%eax 
0x8048426 <main+82>:  mov %eax,%edx 
0x8048428 <main+84>:  shl $0x8,%edx 
0x804842b <main+87>:  mov -0x14(%ebp),%eax 
0x804842e <main+90>:  lea (%edx,%eax,1),%eax 
0x8048431 <main+93>:  mov %eax,-0x18(%ebp) 
0x8048434 <main+96>:  mov -0x18(%ebp),%eax 
0x8048437 <main+99>:  mov %eax,0x4(%esp) 
0x804843b <main+103>:  movl $0x804852c,(%esp) 
0x8048442 <main+110>:  call 0x8048330 <[email protected]> 
0x8048447 <main+115>:  movl $0x0,(%esp) 
0x804844e <main+122>:  call 0x8048340 <[email protected]> 
1

posix.1-2001規定inet_pton()爲這項任務。 這裏也有幾個ms-windows版本。

0
uint32_t getDecimalValueOfIPV4_String(const char* ipAddress) 
{ 
    uint8_t ipbytes[4]={}; 
    int i =0; 
    int8_t j=3; 
    while (ipAddress+i && i<strlen(ipAddress)) 
    { 
     char digit = ipAddress[i]; 
     if (isdigit(digit) == 0 && digit!='.'){ 
      return 0; 
     } 
     j=digit=='.'?j-1:j; 
     ipbytes[j]= ipbytes[j]*10 + atoi(&digit); 

     i++; 
    } 

    uint32_t a = ipbytes[0]; 
    uint32_t b = (uint32_t)ipbytes[1] << 8; 
    uint32_t c = (uint32_t)ipbytes[2] << 16; 
    uint32_t d = (uint32_t)ipbytes[3] << 24; 
    return a+b+c+d; 
} 
+0

我希望這有助於。請享用 !! – 2016-03-18 07:25:15

+0

用代碼添加說明。 – Sandeep 2016-03-18 07:47:26

+0

您初始化一個數組以將所有字節存儲爲零。如果在同一個塊內仍然有數字(即虛線符號),即在點(。)之前[這相當於左邊的二進制shif(x2)],則遍歷ip串,並且遇到的每個數字乘以10。其餘代碼只是將正確的右移應用於每個塊的整數值。 – 2016-03-18 15:45:29