【发布时间】: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 试图帮助我,但没有向我显示编码数据或其他东西?
【问题讨论】:
-
字符串末尾有回车和换行(0x0d 和 0x0a)字符。你可能想去掉那些。例如。 stackoverflow.com/questions/26797739/…