【问题标题】:How do I see the true contents of this string?如何查看该字符串的真实内容?
【发布时间】:2017-04-19 02:29:05
【问题描述】:

请耐心等待,因为这个问题不太容易解释和正确用词。

我正在使用以下代码从 USB 连接的条形码阅读器获取数据,扫描仪工作正常,数据按预期传递,但我的数据库查找失败,我相信它们失败,因为我传递的数据DBLookup 方法不正确,但我不明白为什么,我认为 NSLog 正在帮助我显示明文数据,而实际上它不是,我被困在进一步调试中。

这是我的代码

- (void)didBarcodeDataReceive:(StarIoExtManager *)manager data:(NSData *)data {
NSLog(@"%s", __PRETTY_FUNCTION__);

NSMutableString *text = [NSMutableString stringWithString:@""];

const uint8_t *p = data.bytes;

for (int i = 0; i < data.length; i++) {
    uint8_t ch = *(p + i);
    [text appendFormat:@"%c", (char) ch];
}

NSLog(@"Scanned info as NSData was: %@", data); // raw NSData
//NSString *stringWithData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *stringWithData = [[NSString alloc] initWithBytes:(char *)data.bytes length:data.length encoding:NSUTF8StringEncoding];
NSLog(@"Scanned info as StringFromData was: %@", stringWithData);
NSLog(@"Scanned ch conversion is: %@", text);

int createTransactionResult = -1;

createTransactionResult = [NWBarCodeHelper createTransactionRowFromBarCode:text];

if ([NWTillHelper isDebug] == 1) {
    NSLog(@"mPOP delegate holds barcode: %@", stringWithData);
    if(createTransactionResult != 0) {
        NSLog(@"TransactionListView:mPOPDelegate:createTransactionFrombarCode failed with errorCode %i", createTransactionResult);
    }
}
}

我的调试输出显示正确的数据如下

    2017-04-19 10:19:01.868198 NWMobileTill[3751:1638657] Scanned info as NSData was: <30393235 38333834 33393439 35310d0a>
2017-04-19 10:19:01.868439 NWMobileTill[3751:1638657] Scanned info as StringFromData was: 09258384394951
2017-04-19 10:19:01.868652 NWMobileTill[3751:1638657] Scanned ch conversion is: 09258384394951
2017-04-19 10:19:01.868979 NWMobileTill[3751:1638657] NWBarCodeHelper:createTransactionRowFromBarcode:barcode = 09258384394951
2017-04-19 10:19:01.875938 NWMobileTill[3751:1638657] NWBarcodeHelper:CreateTransactionRowFromBarcode: 0 or more than one row returned, basic data error, item count = 0

但是您可以看到最后一行显示数据库查找失败,我知道该方法是正确的,因为当我使用 iPhone 相机扫描并将该数据传递给相同的方法时,它在相同的条形码上工作得很好,所以它必须从 USB 扫描仪传入的字符串会欺骗我,但我无法理解为什么,我认为 NSLog 试图帮助我,但没有向我显示编码数据或其他东西?

【问题讨论】:

标签: ios utf-8 nslog


【解决方案1】:

您的字符串末尾包含\r\n。看看下面的代码:

unsigned char bytes[] = {0x30, 0x39, 0x32, 0x35, 0x38, 0x33, 0x38, 0x34, 0x33, 0x39 ,0x34, 0x39, 0x35, 0x31, 0x0d, 0x0a};
NSData *data = [NSData dataWithBytes:bytes length:16];
NSString *stringWithData = [[NSString alloc] initWithBytes:(char *)data.bytes length:data.length encoding:NSUTF8StringEncoding];

NSLog(@"%@", stringWithData); // 09258384394951
NSLog(@"%lu", (unsigned long)[stringWithData length]); // 16

// remove \r\n at the end which gets added by the barcode scanner
NSString *string = [stringWithData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"%@", string); // 09258384394951
NSLog(@"%lu", (unsigned long)[string length]); // 14

或者,如果您想使用 appendFormat 方法,您可以在将其添加到字符串之前检查它是否是有效数字,而不是稍后将其删除。

要实际查看字符串的内容,您可以逐个输出字符串中每个字符的代码点,也可以设置断点,Xcode 会在调试器中显示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多