前言
因为要备战蓝桥杯,也是为了以后方便自己查看,总结一下常见进制转换,比如其他进制转十进制,十进制转其他进制,和不同进制之间的转换,一般进制转换分为调用api和手写代码两种。
调用API
十进制转其他进制
转二进制
1 2 3 4
| public static String decimalToBinary(int decimal) { return Integer.toBinaryString(decimal); }
|
转八进制
1 2 3 4
| public static String decimalToOctal(int decimal) { return Integer.toOctalString(decimal); }
|
转十六进制
1 2 3 4
| public static String decimalToHexadecimal(int decimal) { return Integer.toHexString(decimal); }
|
其他进制转十进制
二转十
1 2 3 4
| public static int binaryToDecimal(String binary) { return Integer.parseInt(binary, 2); }
|
八转十
1 2 3 4
| public static int octalToDecimal(String octal) { return Integer.parseInt(octal, 8); }
|
十六转十
1 2 3 4
| public static int hexadecimalToDecimal(String hexadecimal) { return Integer.parseInt(hexadecimal, 16); }
|
不同进制之间的转换
其他进制转二进制
1 2 3 4 5 6 7 8 9 10 11
| public static void main(String[] args) { String octal = "52"; String binaryFromOctal = Integer.toBinaryString(Integer.parseInt(octal, 8)); System.out.println("Octal to Binary: " + binaryFromOctal); String hexadecimal = "2a";
String binaryFromHexadecimal = Integer.toBinaryString(Integer.parseInt(hexadecimal, 16)); System.out.println("Hexadecimal to Binary: " + binaryFromHexadecimal); }
|
二进制转其他进制
1 2 3 4 5 6 7 8 9 10 11 12
| public static void main(String[] args) { String binary = "101010";
String octal = Integer.toString(Integer.parseInt(binary, 2), 8); System.out.println("Binary to Octal: " + octal);
String hexadecimal = Integer.toString(Integer.parseInt(binary, 2), 16); System.out.println("Binary to Hexadecimal: " + hexadecimal);
}
|
手动实现
十进制转其他进制
转二进制
1 2 3 4 5 6 7 8 9
| public static String decimalToBinary(int decimal) { StringBuilder binary = new StringBuilder(); while (decimal > 0) { binary.insert(0, decimal % 2); decimal /= 2; } return binary.toString(); }
|
转八进制
1 2 3 4 5 6 7 8 9
| public static String decimalToOctal(int decimal) { StringBuilder octal = new StringBuilder(); while (decimal > 0) { octal.insert(0, decimal % 8); decimal /= 8; } return octal.toString(); }
|
转十六进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static String decimalToHexadecimal(int decimal) { StringBuilder hexadecimal = new StringBuilder(); while (decimal > 0) { int remainder = decimal % 16; if (remainder < 10) { hexadecimal.insert(0, (char) (remainder + '0')); } else { hexadecimal.insert(0, (char) (remainder - 10 + 'A')); } decimal /= 16; } return hexadecimal.toString(); }
|
其他进制转十进制
二转十
1 2 3 4 5 6 7 8 9 10 11
| public static int binaryToDecimal(String binary) { int decimal = 0; int power = 0; for (int i = binary.length() - 1; i >= 0; i--) { int digit = binary.charAt(i) - '0'; decimal += digit * Math.pow(2, power); power++; } return decimal; }
|
八转十
1 2 3 4 5 6 7 8 9 10 11
| public static int octalToDecimal(String octal) { int decimal = 0; int power = 0; for (int i = octal.length() - 1; i >= 0; i--) { int digit = octal.charAt(i) - '0'; decimal += digit * Math.pow(8, power); power++; } return decimal; }
|
十六转十
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static int hexadecimalToDecimal(String hexadecimal) { int decimal = 0; int power = 0; for (int i = hexadecimal.length() - 1; i >= 0; i--) { char digit = hexadecimal.charAt(i); if (digit >= '0' && digit <= '9') { decimal += (digit - '0') * Math.pow(16, power); } else if (digit >= 'A' && digit <= 'F') { decimal += (digit - 'A' + 10) * Math.pow(16, power); } power++; } return decimal; }
|
不同进制之间的转换
其他进制转二进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static String octalToBinary(String octalNumber) { StringBuilder binaryNumber = new StringBuilder(); for (int i = 0; i < octalNumber.length(); i++) { char digit = octalNumber.charAt(i); int decimal = Character.getNumericValue(digit); String binary = Integer.toBinaryString(decimal); binaryNumber.append(binary); } return binaryNumber.toString(); }
public static String hexadecimalToBinary(String hexadecimalNumber) { StringBuilder binaryNumber = new StringBuilder(); for (int i = 0; i < hexadecimalNumber.length(); i++) { char digit = hexadecimalNumber.charAt(i); int decimal = Character.digit(digit, 16); String binary = Integer.toBinaryString(decimal); binaryNumber.append(binary); } return binaryNumber.toString(); }
|
二进制转其他进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public static String binaryToOctal(String binaryNumber) { StringBuilder octalNumber = new StringBuilder(); int length = binaryNumber.length(); int padding = length % 3; if (padding != 0) { for (int i = 0; i < 3 - padding; i++) { octalNumber.append("0"); } binaryNumber = octalNumber + binaryNumber; length += (3 - padding); } octalNumber.setLength(0); for (int i = 0; i < length; i += 3) { String triplet = binaryNumber.substring(i, i + 3); int decimal = Integer.parseInt(triplet, 2); octalNumber.append(decimal); } return octalNumber.toString(); }
public static String binaryToHexadecimal(String binaryNumber) { StringBuilder hexadecimalNumber = new StringBuilder(); int length = binaryNumber.length(); int padding = length % 4; if (padding != 0) { for (int i = 0; i < 4 - padding; i++) { hexadecimalNumber.append("0"); } binaryNumber = hexadecimalNumber + binaryNumber; length += (4 - padding); } hexadecimalNumber.setLength(0); for (int i = 0; i < length; i += 4) { String quadruplet = binaryNumber.substring(i, i + 4); int decimal = Integer.parseInt(quadruplet, 2); String hex = Integer.toHexString(decimal).toUpperCase(); hexadecimalNumber.append(hex); } return hexadecimalNumber.toString(); }
|