Apple开发_【十六进制_字符串】转【十进制_整形】的相互转换

// 【十六进制_字符串】转【十进制_整形】
- (NSInteger)hex_To_Int:(NSString *)hex_string {
    if ([hex_string hasPrefix:@"#"]) {
        hex_string = [hex_string substringFromIndex:1];
    }
    unsigned long long decimal_value;
    [[NSScanner scannerWithString:hex_string] scanHexLongLong:&decimal_value];
    return decimal_value;
}

// 【十进制_整形】转【十六进制_字符串】
- (NSString *)dec_To_Hex:(NSInteger)decimal {
    NSString *dec_string = [[NSString alloc] initWithFormat:@"%1lx", (long)decimal];
    NSString *hex_string = [NSString stringWithFormat:@"%@", dec_string];
    
    hex_string = [hex_string uppercaseString];
    if (hex_string.length == 1) {
        hex_string = GC_String(@"00000%@", hex_string);
    }
    else if (hex_string.length == 2) {
        hex_string = GC_String(@"0000%@", hex_string);
    }
    else if (hex_string.length == 3) {
        hex_string = GC_String(@"000%@", hex_string);
    }
    else if (hex_string.length == 4) {
        hex_string = GC_String(@"00%@", hex_string);
    }
    else if (hex_string.length == 5) {
        hex_string = GC_String(@"0%@", hex_string);
    }

    return hex_string;
}
posted @ 2024-11-04 15:38  CH520  阅读(9)  评论(0编辑  收藏  举报