如何查看此字符串的真实内容?

How do I see the true contents of this string?

请耐心等待,因为这个问题不是那么容易解释和正确表达的。

我正在使用以下代码从 USB 连接的条码中获取数据 reader,扫描仪工作正常,数据按预期传入,但我的数据库查找失败,我认为它们失败是因为我传递给 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 试图帮助我,但没有向我显示编码数据或其他东西?

您的字符串末尾包含一个 \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 将在调试器中显示它: