对于我的应用程序,我想将NSString转换为包含等效二进制文件的NSMutableString。所有空格必须放在适当的位置。
我发现了一个将字符串转换为二进制的代码段,但是所有空格都被排除在外。看起来或多或少是这样的:
NSMutableString *str2 = [NSMutableString stringWithFormat:@""];
for(NSInteger numberCopy = str; numberCopy > 0; numberCopy >>=1) {
//Prepend "0" or "1", depending on the bit
[str2 insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
Str是输入,str2是输出。
对于包含空格的版本,我有一个数组将输入字符串除以componentsSeparatedByString: @" "
,然后将二进制转换器代码片段(上方)划分为每个数组对象。我重新添加空格。
但是,我收到控制台错误
*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]
其次(尽管我认为它不重要):
0 CoreFoundation 0x00007fff84a08b06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8c72f3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff849a58ec -[__NSArrayM objectAtIndex:] + 252
3 G-Clock 0x0000000100001ff1 -[GCAppController tick:] + 1937
4 Foundation 0x00007fff8a011d05 __NSFireDelayedPerform + 358
5 CoreFoundation 0x00007fff849c5804 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
6 CoreFoundation 0x00007fff849c531d __CFRunLoopDoTimer + 557
7 CoreFoundation 0x00007fff849aaad9 __CFRunLoopRun + 1529
8 CoreFoundation 0x00007fff849aa0e2 CFRunLoopRunSpecific + 290
9 HIToolbox 0x00007fff83bbdeb4 RunCurrentEventLoopInMode + 209
10 HIToolbox 0x00007fff83bbdb94 ReceiveNextEventCommon + 166
11 HIToolbox 0x00007fff83bbdae3 BlockUntilNextEventMatchingListInMode + 62
12 AppKit 0x00007fff88b7a533 _DPSNextEvent + 685
13 AppKit 0x00007fff88b79df2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
14 AppKit 0x00007fff88b711a3 -[NSApplication run] + 517
15 AppKit 0x00007fff88b15bd6 NSApplicationMain + 869
16 G-Clock 0x0000000100000ea2 main + 34
17 libdyld.dylib 0x00007fff864fc7e1 start + 0
18 ??? 0x0000000000000003 0x0 + 3
)
我不知道出什么问题了,更不用说如何更改它了。
我的代码,其中str是输入,而str2是输出:
//Convert to binary
NSMutableString *str2 = [NSMutableString stringWithFormat:@""];
int count = -1;
NSArray *array = [str componentsSeparatedByString: @" "];
NSUInteger numObjects = [array count];
//Converts each object of array into binary separately, so spaces remain intact.
while (1) {
++count;
NSString *string = array[count];
NSMutableString *mutStr = [NSMutableString stringWithFormat:@""];
for (NSInteger numberCopy = [string intValue]; numberCopy > 0; numberCopy >>=1) {
//Prepend "0" or "1", depending on the bit
[mutStr insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
if (numObjects != count + 1) {
//Add space if this is not last run through
[mutStr appendString: @" "];
} else break;
[str2 appendString: mutStr];
}
您在循环中有两个地方count
是递增的。还mutStr
必须附加到str2
在任何情况下。
但我会使用for循环:
for (count = 0; count < numObjects; count++) {
NSString *string = array[count];
NSMutableString *mutStr = [NSMutableString stringWithFormat:@""];
for (NSInteger numberCopy = [string intValue]; numberCopy > 0; numberCopy >>=1) {
//Prepend "0" or "1", depending on the bit
[mutStr insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
[str2 appendString: mutStr];
if (numObjects != count + 1) {
//Add space if this is not last run through
[str2 appendString: @" "];
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句