Decoding the scanned barcode value to int value

可紊 提交于 2020-01-16 00:54:12

问题


When I scan the barcode and I get some value if it is Equal=2 then I need to display with == and if it is Equal=3 then I need to display with = and if the value is 4 then invalid.

But Scanned Barcode are of integer value -- when decode using NSASCII it is displaying only till value 127 after that it is showing invalid results. Eg: if my Barcode value = 9699 the result value=jem then my added result value=jem= actualstring value=asc value id only showing 37

Here is my code:

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

}

回答1:


Since someone revived this question's comments, i'll revive this entire post.

You shouldn't go through NSData to create an NSString from something you already have, and you're probably losing something along the way. Go directly to NSString using stringWithFormat. Also, ASCII will come back and byte you later, if you have a choice, use 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*

Secondly, I looked into the SDK and it says symbol.data is already an NSString*. Depending on what you want, you may not need to do anything. If you do end up needing to change encoding, make sure you understand why you need to (one good reason is "the rest of the application uses NS****StringEncoding").

Also make sure you compare strings the correct "Objective-C" way:

[actualString isEqualToString: testString];

NOT actualString == testString;



来源:https://stackoverflow.com/questions/10244321/decoding-the-scanned-barcode-value-to-int-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!