[iOS development] binary, decimal, sixteen hexadecimal conversion method

[iOS development] binary, decimal, sixteen hexadecimal conversion method
Xcode

Here is a list of binary, decimal, sixteen hexadecimal conversion methods, and sometimes may be used to, the need for a friend can collect it.


1. decimal to binary

@param decimal / * * decimal to binary decimal binary number @return * + (NSString *) getBinaryByDecimal: decimal NSString (NSInteger) {*binary = "@"; while (decimal) {binary = "%ld" [[NSString stringWithFormat:@, decimal 2] stringByAppendingString:binary] if% (decimal / < 2; 1) decimal = {break}; 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"]} = [mStr stringByAppendingString:binary]; binary return binary;}};

2. decimal to sixteen decimal

@param decimal / * * sixteen hexadecimal decimal decimal number sixteen hexadecimal number @return * + (NSString *) getHexByDecimal: (NSInteger) decimal NSString *hex {=@ "; NSString *letter; NSInteger number; for (int i = 0; i< 9; i++) {number = decimal = decimal / decimal% 16; 16; switch (number) {case 10: letter =@" A "; break case; 11: letter; break; =@" B "case 12: letter" C "=@; break; case 13: letter =@" D "; break case; 14: letter =@" E "; break case; 15: letter F; break; =@ default: letter = [NSString stringW IthFormat:@%ld, number] hex stringByAppendingString:hex];} = [letter; if (decimal = = 0) {break}}}; return hex;

3. binary conversion to sixteen decimal

Convert sixteen hex binary / * * @param binary binary number @return sixteen hexadecimal number * + (NSString *) getHexByBinary: (* NSString) binary *binaryDic alloc] initWithCapacity:16] {NSMutableDictionary = [[NSMutableDictionary; [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 "setObject:@" 8 "]; [binaryDic forKey:@" 1000 "];" [binaryDic setObject:@ 9 "forKey:@" 1001 "setObject:@" [binaryDic]; 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 stringByAppendingString:binary] = [mStr;} NSString *hex = @ "for" (int; i=0; i< binary.length; i+=4) {NSString *key = [binary substringWithRange:NSMakeRange (I, 4); hex = [hex stringByAppendingString:[binaryDic objectForKey:key]] return hex;}};

4., sixteen hexadecimal converted to binary

Sixteen / * * converted to hexadecimal binary @param hex sixteen hexadecimal number @return binary number * + (NSString *) getBinaryByHex: (* NSString) hex *hexDic alloc] initWithCapacity:16] {NSMutableDictionary = [[NSMutableDictionary; [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 "setObject:@" 1000 "[hexDic"; 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& lt; [hex length]; i++ *key = [hex substringWithRange:NSMakeRange) {NSString (I, 1); binary = [binary stringByAppendingString:[hexDic objectForKey:key]] return binary;}};

5. binary to decimal

@param binary / * * binary converted to decimal binary number decimal number @return * + (NSInteger) getDecimalByBinary: (NSString * binary) {NSInteger decimal = 0; for (int i=0; i< binary.length; i++) {NSString *number = [binary substringWithRange:NSMakeRange (binary.length - I - 1, 1]); if ([number "isEqualToString:@ 1" ") {decimal + = pow (2, I);}}} return decimal;

About so much, there are other needs can be added to sixteen hexadecimal conversion to decimal words, according to the above methods sixteen hexadecimal conversion to binary to decimal conversion after the line, there is a need of a friend of me!

In the future, you will be grateful for the current self, and the development of their own readers and the road is infinitely better.

My portal: blogs, Jane books, micro-blog, GitHub.