2017-04-19 19 views
0

이 질문은 설명하고 단어를 올바르게 설명하기가 쉽지 않으므로 여기에 나와 함께하시기 바랍니다.이 문자열의 실제 내용은 어떻게 볼 수 있습니까?

다음 코드를 사용하여 USB 연결 바코드 판독기에서 데이터를 가져 오는 중 스캐너가 올바르게 작동하고 예상대로 데이터가 전달되지만 DB 조회가 실패하고 통과하는 데이터 때문에 실패하고있는 것 같습니다. DBLookup 메쏘드에 잘못되었습니다. 그러나 나는 왜 그것을 볼 수 없습니다. 실제로 NSLLog가 클리어 텍스트 데이터를 보여줄 수 있도록 도와 주었다고 생각합니다. 그리고 나는 더 이상 디버깅을해야 할 것입니다. 여기

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 

을 다음과 같이

- (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); 
    } 
} 
} 

내 디버그 출력 올바른 데이터를 보여줍니다

내 코드입니다 그러나 마지막 행을 볼 수있는 나는 방법을 알고, 실패한 DB 조회를 보여줍니다 올바른 원인은 내가 아이폰 카메라를 사용하여 스캔하고 동일한 바코드에 잘 작동하는 동일한 방법으로 데이터를 전달하므로 날 속일 수 있지만 내가 할 수없는 USB 스캐너에서 전달되는 문자열과 뭔가 있어야합니다 왜 그리고 NSLog가 시도하고 있다고 생각하는지 이해하기. o 나를 도와 주지만 인코딩 된 데이터 또는 다른 것을 보여주지 않습니까?

+0

당신 문자열 끝에 캐리지 리턴 및 줄 바꿈 (0x0d 및 0x0a) 문자가 있어야합니다. 당신은 아마 그것들을 벗기고 싶을 것입니다. 예. http://stackoverflow.com/questions/26797739/does-swift-have-a-trim-method-on-string – Paulw11

답변

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 접근 방식을 사용하려면 유효한 숫자가 문자열에 추가하는 대신 나중에 제거하기 전에 경우 당신은 확인할 수 있습니다.

실제로 문자열의 내용을 참조하십시오 할 수 있습니다 중 하나를 출력하여 문자열 하나의 각 문자의 코드 포인트 또는 그냥 중단 점을 설정하고 엑스 코드 디버거에 표시됩니다 : enter image description here