我正在尝试将我的十六进制值转换为二进制值,但我面临的问题很小。因为我是新手,试图学习我的错误。
我的代码:
NSMutableString *str;
NSString *dd = @"192:168:1:2:0B:2:D:00";
NSCharacterSet *donotwant1 = [NSCharacterSet characterSetWithCharactersInString:@":""];
dd =[[dd componentsSeparatedByCharactersInSet:donotwant1] componentsJoinedByString:@" "];
NSMutableArray *array = [[dd componentsSeparatedByString:@" "] mutableCopy];
[array removeObjectAtIndex:0];
//NSLog(@"%@",array);
for (int j=0; j<[array count]; j++) {
NSScanner *scan = [NSScanner scannerWithString:[array objectAtIndex:j]];
unsigned int i=0;
if ([scan scanHexInt:&i]) {
// NSLog(@"numbner is %ustr", i);
}
NSInteger theNumber = i;
str = [NSMutableString string];
for(NSInteger numberCopy = theNumber; numberCopy > 0; numberCopy >>= 1) {
// Prepend "0" or "1", depending on the bit
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
[array removeObjectAtIndex:j];
[array insertObject:str atIndex:j];
}
}
NSLog(@"Binary version: %@", array);
我越来越
1,1100,11001111,1111,1111,11101111......
在我的代码中 0 值被消除。我想要 8bits like(00000001,00001100 .....) 谁能告诉我原因