iOS 十六进制的相加取反

ios中将NSstring字符串转换成char类型

NSString *string = [NSString stringWithFormat:@"5D"];
        
const char *result = [string UTF8String];

 

char字符转成NSstring

char a[10] = "3Er4";
        
NSString *string = [NSString stringWithUTF8String:a];

 

将char类型字符转成十进制类型,然后需要什么样的进制类型都可以直接转换了

unsigned long num = strtoul(result, 0, 16);
        
NSLog(@"%lu",num);
        
NSLog(@"%0lx",num);
        
NSLog(@"%0lX",num);

%0x----是保存成十六进制的(字母是小写)

%0X----是保存成十六进制的(字母是大写)

 

十六进制相加 取反

unsigned long num1 = strtoul([@"5D" UTF8String],0,16);
unsigned long num2 = strtoul([@"EE" UTF8String],0,16);
unsigned long num3 = strtoul([@"22" UTF8String],0,16);
unsigned long num4 = strtoul([@"01" UTF8String],0,16);
    
// 进制相加
Byte A = num1 + num2 + num3 + num4;
// 取反
Byte B = ~A;

// 转成十六进制
NSString *string = [NSString stringWithFormat:@"%0X",B];

 

posted @ 2015-10-14 11:28  越过三  阅读(3916)  评论(0编辑  收藏  举报