iOS development-in-process conversion

Source: Internet
Author: User

1, decimal conversion to Binary
/**十进制转换为二进制@param decimal 十进制数@return 二进制数*/+ (NSString *)getBinaryByDecimal:(NSInteger)decimal {  NSString *binary = @"";  while (decimal) {    binary = [[NSString stringWithFormat:@"%ld", decimal % 2] stringByAppendingString:binary];    if (decimal / 2 < 1) {      break;    }    decimal = decimal / 2 ;    }  if (binary.length % 4 != 0) {    NSMutableString *mStr = [[NSMutableString alloc]init];    for (int i = 0; i < 4 - binary.length % 4; i++) {      [mStr appendString:@"0"];    }    binary = [mStr stringByAppendingString:binary];  }  return binary;}
2. Decimal conversion to hexadecimal
/**将十进制转化为十六进制@param decimal 10进制 int类型@return 16进制*/+ (NSString*)changDecimalToHex:(NSInteger)decimal {  NSString* intDecimalStr = [[NSString alloc] initWithFormat:@"%1lx", (long)decimal];  NSString* hexString = [NSString stringWithFormat:@"%@", intDecimalStr];  if (hexString.length == 5) {    return [[NSString stringWithFormat:@"0%@", hexString] uppercaseString];  } else {    return [hexString uppercaseString];  }}
3. binary conversion to hexadecimal
/** binary converted to hexadecimal @param binary binary @return hexadecimal number */+ (NSString *) Gethexbybinary: (NSString *) binary {nsmutabledic    Tionary *binarydic = [[Nsmutabledictionary alloc] initwithcapacity:16];    [Binarydic setobject:@ "0" forkey:@ "0000"];    [Binarydic setobject:@ "1" forkey:@ "0001"];    [Binarydic setobject:@ "2" forkey:@ "0010"];    [Binarydic setobject:@ "3" forkey:@ "0011"];    [Binarydic setobject:@ "4" forkey:@ "0100"];    [Binarydic setobject:@ "5" forkey:@ "0101"];    [Binarydic setobject:@ "6" forkey:@ "0110"];    [Binarydic setobject:@ "7" forkey:@ "0111"];    [Binarydic setobject:@ "8" forkey:@ "1000"];    [Binarydic setobject:@ "9" forkey:@ "1001"];    [Binarydic setobject:@ "A" forkey:@ "1010"];    [Binarydic setobject:@ "B" forkey:@ "1011"];    [Binarydic setobject:@ "C" forkey:@ "1100"];    [Binarydic setobject:@ "D" forkey:@ "1101"];    [Binarydic setobject:@ "E" forkey:@ "1110"];        [Binarydic setobject:@ "F" forkey:@ "1111"]; if (binary.length% 4 = 0) {nsmutablestring *mSTR = [[nsmutablestring alloc]init];;        for (int i = 0; i < 4-binary.length% 4; i++) {[Mstr appendstring:@ "0"];    } binary = [Mstr stringbyappendingstring:binary];    } nsstring *hex = @ "";        for (int i=0; i<binary.length; i+=4) {nsstring *key = [Binary Substringwithrange:nsmakerange (I, 4)];        NSString *value = [Binarydic Objectforkey:key];        if (value) {hex = [hex Stringbyappendingstring:value]; }} return hex;}
4, hexadecimal to binary
/** 16 binary to binary @param hexstring hexadecimal number @return binary number */+ (NSString *) getbinarybyhexstring: (NSString *) hexstring {Nsmuta    Bledictionary *hexdic = [[Nsmutabledictionary alloc] initwithcapacity:16];    [Hexdic setobject:@ "0000" forkey:@ "0"];    [Hexdic setobject:@ "0001" forkey:@ "1"];    [Hexdic setobject:@ "0010" forkey:@ "2"];    [Hexdic setobject:@ "0011" forkey:@ "3"];    [Hexdic setobject:@ "0100" forkey:@ "4"];    [Hexdic setobject:@ "0101" forkey:@ "5"];    [Hexdic setobject:@ "0110" forkey:@ "6"];    [Hexdic setobject:@ "0111" forkey:@ "7"];    [Hexdic setobject:@ "forkey:@" 8 "];    [Hexdic setobject:@ "1001" forkey:@ "9"];    [Hexdic setobject:@ "1010" forkey:@ "A"];    [Hexdic setobject:@ "1011" forkey:@ "B"];    [Hexdic setobject:@ "1100" forkey:@ "C"];    [Hexdic setobject:@ "1101" forkey:@ "D"];    [Hexdic setobject:@ "1110" forkey:@ "E"];        [Hexdic setobject:@ "1111" forkey:@ "F"];    NSString *binary = @ ""; for (int i = 0; i < [hexstring length]; i++) {NSString *key =[HexString Substringwithrange:nsmakerange (I, 1)];        NSString *value = [Hexdic objectForKey:key.uppercaseString];        if (value) {binary = [binary stringbyappendingstring:value]; }} return binary;}
5, hexadecimal to decimal
/** 将十六进制转化为十进制  @param hexString 16进制 nsstring类型 @return 10进制 */+ (NSInteger)changHexToDecimal:(NSString*)hexString {    NSString* temp10 = [NSString stringWithFormat:@"%lu", strtoul([hexString UTF8String], 0, 16)];    int cycleNumber = [temp10 intValue];    return cycleNumber;}

iOS development-in-process conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.