500 → "111110100 "
int num = 500;
------------------------------------------------------------------------
Integer 提供方法
------------------------------------------------------------------------
# 将数字转换成 10 进制字符串
String s = Integer.toString(num);
# 将数字转换成 2 进制字符串
String s1 = Integer.toBinaryString(num);
String ss1 = Integer.toString(num, 2);
# 将数字转换成 8 进制字符串
String s2 = Integer.toOctalString(num);
String ss2 = Integer.toString(num, 8);
# 将数字转换成 16 进制字符串
String s3 = Integer.toHexString(num);
String ss3 = Integer.toString(num, 16);
------------------------------------------------------------------------
String提供方法,适用于8进制,10进制,16进制,但不适用于二进制方法
------------------------------------------------------------------------
# %d表示将整数格式化为10进制整数
String format = String.format("%d", num);
# %o表示将整数格式化为8进制整数
String format1 = String.format("%o", num);
# %x表示将整数格式化为16进制整数
String format2 = String.format("%x", num);
# %X表示将整数格式化为16进制整数,并且字母变成大写形式
String format3 = String.format("%X", num);
------------------------------------------------------------------------
递归直接输出
------------------------------------------------------------------------
public static void fun(int num) {
int goInside = num / 2;
if (goInside >= 1) {
fun(goInside);
}
int remainder = num % 2;
System.out.print(remainder);
}
fun(500); → 111110100
------------------------------------------------------------------------
递归收集到集合
------------------------------------------------------------------------
public static void fun1(Deque<Integer> res, int num) {
int goInside = num / 2;
if (goInside >= 1) {
fun1(res, goInside);
}
int remainder = num % 2;
res.addLast(remainder);
}
Deque<Integer> res = new LinkedList();
fun1(res, 500);
String collect = res.stream().map(String::valueOf).collect(Collectors.joining(""));
------------------------------------------------------------------------
递归拼接为String
------------------------------------------------------------------------
public static String fun3(int num) {
if (num == 0) {
return "";
}
int remainder = num % 2;
return fun3(num / 2) + remainder;
}
String s = fun3(500);
System.out.println(s);
System.out.println(Integer.toBinaryString(500));
“1001101” → 77
------------------------------------------------------------------------
# res = res * 2 + numericValue;
------------------------------------------------------------------------
public static void binaryToDecimal3(String binaryint) {
char[] chars = binaryint.toCharArray();
int res = 0;
for (int i = 0; i < chars.length; i++) {
int numericValue = Character.getNumericValue(chars[i]);
res = res * 2 + numericValue;
}
System.out.println("这个二进制的十进制数为:" + res);
}
public static void binaryToDecimal3(String binaryint) {
char[] chars = binaryint.toCharArray();
Integer reduce = IntStream.range(0, chars.length).mapToObj(i -> Character.getNumericValue(chars[i]))
.reduce(0, (result, numericValue) -> result * 2 + numericValue);
System.out.println("这个二进制的十进制数为:" + reduce);
}
binaryToDecimal3("1001101");
binaryToDecimal4("1001101");
这个二进制的十进制数为:77
------------------------------------------------------------------------
# res += numericValue * Math.pow(2, index++);
------------------------------------------------------------------------
public static void binaryToDecimal2(String binaryint) {
int res = 0;
int index = 0;
char[] chars = binaryint.toCharArray();
for (int i = chars.length - 1; i >= 0; i--) {
int numericValue = Character.getNumericValue(chars[i]);
res += numericValue * Math.pow(2, index++);
}
System.out.println("这个二进制的十进制数为:" + res);
}
binaryToDecimal2("1001101");
这个二进制的十进制数为:77
1001101 → 77
public static void binaryToDecimal1(int binaryint) {
int res = 0;
int remainder = 0;
int index = 0;
while (binaryint != 0) {
remainder = binaryint % 10;
binaryint /= 10;
res += remainder * Math.pow(2, index++);
}
System.out.println("这个二进制的十进制数为:" + res);
}
binaryToDecimal1("1001101");
这个二进制的十进制数为:77
“0xAA” → 170
private static Map<Character, Integer> map = new HashMap<>() {{
put('0', 0);
put('1', 1);
put('2', 2);
put('3', 3);
put('4', 4);
put('5', 5);
put('6', 6);
put('7', 7);
put('8', 8);
put('9', 9);
put('A', 10);
put('B', 11);
put('C', 12);
put('D', 13);
put('E', 14);
put('F', 15);
put('a', 10);
put('b', 11);
put('c', 12);
put('d', 13);
put('e', 14);
put('f', 15);
}};
public static void hexToDecimal1(String number) {
number = number.substring(2);
int res = 0;
for (char ch : number.toCharArray()) {
res = res * 16 + map.get(ch);
}
System.out.println(res);
}
public static void hexToDecimal1(String number) {
number = number.substring(2);
char[] chars = number.toCharArray();
int reduce = IntStream.range(0, chars.length).map(i -> map.get(chars[i])).reduce(0, (a, b) -> a * 16 + b);
System.out.println(reduce);
}
public static void hexToDecimal1(String number) {
number = number.substring(2);
String finalNum = number;
int reduce = IntStream.range(0, number.length()).map(i -> map.get(finalNum.charAt(i))).reduce(0, (a, b) -> a * 16 + b);
System.out.println(reduce);
}
public static void hexToDecimal2(String number) {
number = number.substring(2);
int res = 0;
int index = 0;
char[] chars = number.toCharArray();
for (int i = chars.length - 1; i >= 0; i--) {
res += map.get(chars[i]) * Math.pow(16, index++);
}
System.out.println(res);
}
hexToDecimal1("0xAA");
hexToDecimal2("0xAA");
参考
Java中二进制转换的多种方法