Decoding the scanned barcode value to int value

2019-07-23 04:09发布

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条回答
再贱就再见
2楼-- · 2019-07-23 04:37

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;

查看更多
登录 后发表回答