[英]Decoding the scanned barcode value to int value
當我掃描條形碼並獲得一些值(如果它等於Equal = 2)時,我需要顯示== ,如果它等於Equal = 3,那么我需要顯示= ,如果值是4,則無效。
但是“掃描條形碼”是整數值-使用NSASCII
解碼時,它僅顯示直到值127
之后才顯示無效結果。 例如:如果我的條形碼值= 9699
的結果值= jem
然后我的相加結果值= jem=
actualstring值= %å
ASC值id僅示出37
這是我的代碼:
- (void) readerView:(ZBarReaderView *)view didReadSymbols:(ZBarSymbolSet *)syms fromImage:(UIImage *)img
{
// do something useful with results -- cool thing is that you get access to the image too
for (ZBarSymbol *symbol in syms) {
[resultsBox setText:symbol.data];
if ([resultsBox.text length] == 2) {
addedresult.text = [resultsBox.text stringByAppendingString:@"=="];
} else if ([resultsBox.text length] == 3) {
addedresult.text = [resultsBox.text stringByAppendingString:@"="];
} if ([resultsBox.text length] >= 4) {
addedresult.text = @"Invalid";
}
[Base64 initialize];
NSString *myString = [[NSString alloc]initWithString:addedresult.text];
NSData * data = [Base64 decode:myString];
NSString * actualString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@",actualString);
labeltext.text= actualString;
int asc = [actualString characterAtIndex:0];
label.text = [NSString stringWithFormat:@"%d", asc];
[actualString release];
break;
}
}
由於有人復活了該問題的評論,因此我將復述整個帖子。
您不應該通過NSData從已經擁有的東西創建一個NSString,並且在此過程中可能會丟失一些東西。 使用stringWithFormat直接轉到NSString。 同樣,ASCII將在稍后返回並字節給您,如果您可以選擇的話,請使用UTF8。
NSString *actualStringUTF8 = [NSString stringWithFormat:@"%@",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSString *actualStringASCII = [NSString stringWithFormat:@"%@",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",actualStringUTF8);
NSLog(@"%c",[actualStringUTF8 UTF8String]); //This is a const char*
其次,我查看了SDK,並說symbol.data已經是一個NSString *。 根據您的需要,您可能不需要執行任何操作。 如果確實需要更改編碼,請確保您了解需要這樣做的原因(一個很好的原因是“應用程序的其余部分使用NS **** StringEncoding”)。
還要確保以正確的“ Objective-C”方式比較字符串:
[actualString isEqualToString: testString];
不是actualString == testString;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.