ios - 如何查看该字符串的真实内容?

标签 ios utf-8 nslog

请耐心听我说,因为这个问题不太容易解释和正确表达。

我正在使用以下代码来从 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 试图帮助我,但没有向我显示编码数据或其他内容?

最佳答案

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

关于ios - 如何查看该字符串的真实内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485282/

相关文章:

PHP:连接长字符串时丢失字符

Java : save data from UTF-8 file into Sql Server 2008

ios - NSLog 的奇怪行为

ios - 如何使用 HKObserverquery 读取 iOS Healthkit 血压(收缩压,舒张压)?

ios - 应用程序终止时执行后台任务

ios - 将自定义图标字形附加到 NSAttributedString

ios - NSLog/Log4Cocoa 控制台日志需要保存在文件中

ios - 使用迦太基在 Xcode 中导入 ZipFoundation 不起作用

windows - 在 perl 脚本中或与 perl 脚本一起使用 utf8

swift - 相同的 Int 在 Swift 中有不同的值,神秘的谜语