コードを公開しますけど,いじめないでくださいね.
#import <Foundation/Foundation.h>
int main(void){
int prefixNumber = 0;
int i,j,temp;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *addressString = [NSString string];
//NSString *subnetString = @"255.255.255.0";
NSString *subnetString = @"255.255.255.240";
//NSString *subnetString = @"128.0.0.0";
//NSString *subnetString = @"255.248.0.0";
NSArray *subnetStringItems = [subnetString componentsSeparatedByString:@"."];
for(i = 0; i < 4; i++){
temp = [[subnetStringItems objectAtIndex:i] intValue];
for(j=0;j<8;j++){
prefixNumber += (temp & 1);
temp >>= 1;
}
}
NSLog(@"prefixNumber=%d",prefixNumber);
for(i = 0; i < 4; i++){
if(prefixNumber >= 8){
addressString = [addressString stringByAppendingString: @"255"];
prefixNumber = prefixNumber - 8;
}else if(prefixNumber > 0){
temp = 0;
temp = 255 - ((1 << (8-prefixNumber)) - 1);
addressString = [addressString stringByAppendingString: [ NSString stringWithFormat : @"%d", temp ]];
prefixNumber = 0;
}else{
addressString = [addressString stringByAppendingString: @"0"];
}
if(i<3){
addressString = [addressString stringByAppendingString: @"."];
}
}
NSLog(@"addressString=%@",addressString);
[pool drain];
return 0;
}
どう書く?org IPv4アドレスのマスクの変換
http://ja.doukaku.org/253/nested/
0 件のコメント:
コメントを投稿