`
zjjzmw1
  • 浏览: 1366320 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

NSScaner的用法

    博客分类:
  • iOS
 
阅读更多

//扫描接受完数据,进行的解析,这是核心代码。
- (void)parse
{
    ITTDINFO(@"code %@", _code);
    NSInteger length = [_code length];
    if (58 != length) {
        _valid = FALSE;
        self.message = @"无效的二维码!";
    }
    else {
        _codeInfo = [[MJKCode alloc] init];
        uint outVal;
        NSScanner *scanner;
        NSInteger index = 0;
        //商家编号
        NSString *merchant = [_code substringWithRange:NSMakeRange(index, LENGTH_OF_MERCHANT_ID)];
        index += LENGTH_OF_MERCHANT_ID;
        scanner = [NSScanner scannerWithString:merchant];
        [scanner scanHexInt:&outVal];
        _codeInfo.merchantId = [NSString stringWithFormat:@"%d", outVal];
       
        //日期
        NSString *date = [_code substringWithRange:NSMakeRange(index, LENGTH_OF_DATE)];
        index += LENGTH_OF_DATE;
        NSMutableString *dateString = [[NSMutableString alloc] init];
        //年
        NSInteger subIndex = 0;
        NSString *year = [date substringWithRange:NSMakeRange(subIndex, 2)];
        scanner = [NSScanner scannerWithString:year];
        [scanner scanHexInt:&outVal];
        NSInteger currentYear = [NSDate getCurrentYear];
        [dateString appendFormat:@"%d%d", currentYear/100, outVal];
        subIndex += 2;
       
        //月
        NSString *month = [date substringWithRange:NSMakeRange(subIndex, 2)];
        scanner = [NSScanner scannerWithString:month];
        [scanner scanHexInt:&outVal];
        [dateString appendFormat:@"%d", outVal];
        subIndex += 2;
       
        //日
        NSString *day = [date substringWithRange:NSMakeRange(subIndex, 2)];
        scanner = [NSScanner scannerWithString:day];
        [scanner scanHexInt:&outVal];
        [dateString appendFormat:@"%d", outVal];
        subIndex += 2;
       
        //时
        NSString *hour = [date substringWithRange:NSMakeRange(subIndex, 2)];
        scanner = [NSScanner scannerWithString:hour];
        [scanner scanHexInt:&outVal];
        [dateString appendFormat:@"%d", outVal];
        subIndex += 2;
       
        //分
        NSString *minute = [date substringWithRange:NSMakeRange(subIndex, 2)];
        scanner = [NSScanner scannerWithString:minute];
        [scanner scanHexInt:&outVal];
        [dateString appendFormat:@"%d", outVal];
       
        _codeInfo.date = dateString;
        [dateString release];
       
        //流水号
        NSString *serialNumber = [_code substringWithRange:NSMakeRange(index, LENGTH_OF_SERIAL_NUMBER)];
        index += LENGTH_OF_SERIAL_NUMBER;
        scanner = [NSScanner scannerWithString:serialNumber];
        [scanner scanHexInt:&outVal];
        _codeInfo.serialNumber = [NSString stringWithFormat:@"%d", outVal];
       
        //消费人数
        NSString *consumberNumber = [_code substringWithRange:NSMakeRange(index, LENGTH_OF_CONSUMER_NUMBER)];
        index += LENGTH_OF_CONSUMER_NUMBER;
        scanner = [NSScanner scannerWithString:consumberNumber];
        [scanner scanHexInt:&outVal];
        _codeInfo.consumerNumber = [NSString stringWithFormat:@"%d", outVal];

        //金额
        NSString *amount = [_code substringWithRange:NSMakeRange(index, LENGTH_OF_AMOUNT)];
        index += LENGTH_OF_AMOUNT;
        scanner = [NSScanner scannerWithString:amount];
        [scanner scanHexInt:&outVal];
        _codeInfo.amount = [NSString stringWithFormat:@"%d", outVal];
       
        //校验码
        NSString *vierificationCode = [_code substringWithRange:NSMakeRange(index, LENGTH_OF_VERIFICATION)];
        _codeInfo.verificationCode = [vierificationCode uppercaseString];
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics