将扫描的条形码值解码为int值 [英] Decoding the scanned barcode value to int value

查看:167
本文介绍了将扫描的条形码值解码为int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我扫描条形码时,如果它是 Equal = 2 ,我会得到一些价值,那么我需要显示 == ,如果它是 Equal = 3 ,那么我需要显示 = ,如果该值为4,则无效.

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.

但是扫描的条形码是整数值-使用NSASCII解码时,它仅显示直到值127,之后才显示无效结果.例如:如果我的条形码值= 9699结果值= jem,那么我添加的结果值= jem= 实际字符串值= asc 值id显示37

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

这是我的代码:

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

}

推荐答案

由于有人复活了该问题的评论,因此我将复述整个帖子.

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

您不应该使用NSData从已经拥有的东西创建NSString,并且在此过程中可能会丢失一些东西.使用stringWithFormat直接转到NSString.另外,ASCII将在稍后返回并字节给您,如果您可以选择的话,请使用UTF8.

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*

其次,我查看了SDK,并说symbol.data已经是一个NSString *.根据您的需求,您可能不需要执行任何操作.如果您确实需要更改编码,请确保您了解需要这样做的原因(一个很好的原因是应用程序的其余部分使用NS **** StringEncoding").

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").

还要确保您以正确的"Objective-C"方式比较字符串:

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

[actualString isEqualToString: testString];

NOTactualString == testString;

NOT actualString == testString;

这篇关于将扫描的条形码值解码为int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆