2013-10-12 57 views
0

對於我的應用程序,我想將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]; 
     } 

回答

1

你必須在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: @" "]; 
    } 
} 
+0

這是絕對不會讓我的代碼工作,但不幸的是,它不是解決方案。編輯該文件。 –

+0

@ jazz14456:代碼中出現了另一個錯誤,請參閱更新後的答案。 –

+0

我也在同一時間發佈:)。我會接受你的,因爲你不是提問者,而且你做了第一部分關於'count + 1'而不是'++ count'以及最後的錯誤 –

0

這是一個邏輯錯誤。在@Martin的部分之後,在最後一次運行中,else break位於[array count]被調用的部分之後,並且由於該計數索引處沒有數組對象,程序崩潰了。

while (1) { 
    ++count; 
    if (count == numbObjects) 
     break; 
    ... 
    } 
相關問題