【问题标题】:How can a string be converted into binary with the spaces intact?如何将字符串转换为空格完整的二进制?
【发布时间】:2013-10-20 13:13:00
【问题描述】:

对于我的应用程序,我想将 NSString 转换为包含二进制等效项的 NSMutableString。所有空格都必须放在适当的位置。

我找到了一个可以将字符串转换为二进制的 sn-p,但所有空格都被排除在外。它或多或少看起来像这样:

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: @" ",然后将二进制转换器 sn-p(上图)转换为每个数组对象。我将空格重新添加进去。

但是,我得到控制台错误

*** -[__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];
        }

【问题讨论】:

    标签: objective-c binary space converters


    【解决方案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: @" "];
        }
    }
    

    【讨论】:

    • 那肯定不会让我的代码工作,但不幸的是它不是解决方案。编辑它。
    • @jazz14456:您的代码中还有另一个错误,请参阅更新后的答案。
    • 我也同时发了 :)。我会接受你的,因为你不是提问者,你做了第一部分关于 count + 1 而不是 ++count 以及最后一个错误
    • 在我接受之前,还要将我的答案添加到您的答案中,因为没有该部分将无法工作。
    • @jazz14456:我现在添加了另一个变体,它也可以工作,并且在我看来更容易阅读。
    【解决方案2】:

    这是一个逻辑错误。在 @Martin 的部分之后,在最后一次运行中,else break 在调用 [array count] 的部分之后,并且由于在该计数索引处没有数组对象,因此程序中断了。

    while (1) {
        ++count;
        if (count == numbObjects) 
           break;
        ...
        }
    

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 2013-09-19
      • 2012-01-06
      • 2021-02-24
      相关资源
      最近更新 更多