iOS获取字符串string的异或值

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface HKXORManager : NSObject

+ (HKXORManager *)shareManager;
/**
 * 计算异或值
 * @param string 需要异或的字符串
 * @return 获取异或后的字符串
 */
- (NSString *)stringXOR:(NSString *)string;

@end

NS_ASSUME_NONNULL_END

  

#import "HKXORManager.h"

@implementation HKXORManager

+ (HKXORManager *)shareManager {
    static HKXORManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[HKXORManager alloc] init];
    });
    return instance;
}

- (NSString *)stringXOR:(NSString *)string {
    if (string.length%2) {
        string = [NSString stringWithFormat:@"0%@",string];
    }
    NSString *code = @"00";
    for (int i=0; i<string.length; i=i+2) {
        NSString *subString = [string substringWithRange:NSMakeRange(i,2)];
        code = [self pinxCreator:code withPinv:subString];
    }
    return code;
}

- (NSString *)pinxCreator:(NSString *)pan withPinv:(NSString *)pinv {
    if (pan.length != pinv.length) {
        return nil;
    }
    const char *panchar = [pan UTF8String];
    const char *pinvchar = [pinv UTF8String];
    NSString *temp = [[NSString alloc] init];
    for (int i = 0; i < pan.length; i++) {
        int panValue = [self charToint:panchar[i]];
        int pinvValue = [self charToint:pinvchar[i]];
        temp = [temp stringByAppendingString:[NSString stringWithFormat:@"%X",panValue^pinvValue]];
    }
    return temp;
}

- (int)charToint:(char)tempChar {
    if (tempChar >= 0 && tempChar <=9) {
        return tempChar - 0;
    }
    else if (tempChar >= A && tempChar <= F) {
        return tempChar - A + 10;
    }
    return 0;
}

@end

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...