2011-09-28 131 views
2

例如地址網絡掩碼對的:
如何獲得CIDR標記法10.66.114.0/24
的較低更高的IP地址在這種情況下,答案是:
10.66.114.1 - 10.66.114.254如何獲得IP範圍了CIDR標記法或使用Java

+1

重複的http://stackoverflow.com/questions/2942299 /轉換-CIDR的地址到子網掩碼-一個nd-network-address – bstick12

+1

你的例子是錯誤的。它是10.66.114.0 - 10.66.114.255 – mailq

回答

4

下面是它的要點(我是一個C的傢伙):

unsigned int network = 0x0a427200; /* 10.66.114.0 */ 
unsigned int subnet_length = 24; 

unsigned int mask = 0xfffffff << (32 - subnet_length); 
unsigned int bcast = 0xffffffff >> (32 - subnet_length); 

unsigned int start_ip = (network & mask) + 1; 
unsigned int end_ip = (network & mask) + (bcast - 1); 
-1
#include <ncurses.h> 
// ncurses.h includes stdio.h 
#include <math.h> 
#include <stdlib.h> 

// determine network range of masked ip. eg: 192.168.113.0/25 
// unsigned int network = 0x0a427200; /* 10.66.114.0 */ 
// unsigned int subnet_length = 24; 
// 
// unsigned int mask = 0xfffffff << (32 - subnet_length); 
// unsigned int bcast = 0xffffffff >> (32 - subnet_length); 
// 
// unsigned int start_ip = (network & mask) + 1; 
// unsigned int end_ip = (network & mask) + (bcast - 1); 


int main(int argc, char *argv[]) 
{ 
// char arg[50] = " "; 
    char ipmask[50] = "192.168.113.0/25"; 
// determine network range of masked ip. eg: 192.168.113.0/25 
    unsigned int network = 0xc0a87100; /* 192.168.113.0 */ 
    unsigned int subnet_length = 25; 

    unsigned int mask = 0xfffffff << (32 - subnet_length); 
    unsigned int bcast = 0xffffffff >> (subnet_length); 
// unsigned int bcast = 0xffffffff >> (32 - subnet_length); 

    unsigned int start_ip = (network & mask) + 1; 
    unsigned int end_ip = (network & mask) + (bcast); 

    initscr(); 
    cbreak(); 
    printw("\nipmask %s\nstart %x end %x range %x\n" 
     "press any key\n", ipmask, start_ip, end_ip, end_ip - start_ip + 1); 

    getch(); 
    endwin(); // close ncurses library and restores previous console 
    return(0); 

} 
/*****************************************************************************/ 
// end netmask 

    gcc -Wall netmask.c -o netmask -lm -lncurses 
+0

Errm ...這是C,而不是Java ... – Manu