Binary, Octal, and Hexadecimal Conversions in Java

Convert from Binary, Octal or Hex to Decimal using Integer.parseInt(String input, int radix)

Use Integer.parseInt(String input, int radix) to convert from any type of number to an Integer.

String binaryNumber = "10101";
int decimal1 = Integer.parseInt(binaryNumber, 2);

System.out.println(binaryNumber + " in Base 10 : " + decimal1);

String octalNumber = "456";
int decimal2 = Integer.parseInt(octalNumber, 8);

System.out.println(octalNumber + " in Base 10 : " + decimal2);

String hexNumber = "ABCD";
int decimal3 = Integer.parseInt(hexNumber, 16);

System.out.println(hexNumber + " in Base 10 : " + decimal3);

Output:

10101 in Base 10 : 21
456 in Base 10 : 302
ABCD in Base 10 : 43981

Convert from Decimal to Binary, Octal or Hex using Integer.toString(int input, int radix)

Use Integer.toString(int input, int radix) to convert from an Integer to any type of base number.

Integer decimal1 = 21;
String binaryNumber = Integer.toString(decimal1, 2);

System.out.println(decimal1 + " in Base 2 : " + binaryNumber);

Integer decimal2 = 302;
String octalNumber = Integer.toString(decimal2, 8);

System.out.println(decimal2 + " in Base 8 : " + octalNumber);

Integer decimal3 = 43981;
String hexNumber = Integer.toString(decimal3, 16);

System.out.println(decimal2 + " in Base 16 : " + hexNumber);

Output:

21 in Base 2 : 10101
302 in Base 8 : 456
43981 in Base 16 : abcd

Convert from Decimal to Binary, Octal or Hex using Integer.toXXXString(int)

Integer class does provide lots of unitily methods to be consumed directly. Check out them in java doc.

Integer decimal1 = 21;
System.out.println(decimal1 + " in Base 2 : " + Integer.toBinaryString(decimal1));

Integer decimal2 = 302;
System.out.println(decimal2 + " in Base 8 : " + Integer.toOctalString(decimal2));

Integer decimal3 = 43981;
System.out.println(decimal3 + " in Base 16 : " + Integer.toHexString(decimal3));

Output:

21 in Base 2 : 10101
302 in Base 8 : 456
43981 in Base 16 : abcd
时间: 2024-10-04 02:39:18

Binary, Octal, and Hexadecimal Conversions in Java的相关文章

Flatten Binary Tree to Linked List leetcode java

题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空.注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子.整个程序需要维护一个全局变量,保

leecode Binary Tree Level Order Traversal II java

做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没. https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ import java.util.*; /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNod

Binary Tree Maximum Path Sum leetcode java

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

leetcode 107 Binary Tree Level Order Traversal II ----- java

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up level order

Binary Tree Level Order Traversal - leetcode - java

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]] DFS实现

107. Binary Tree Level Order Traversal II Java Solutions

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

[LeetCode] 103. Binary Tree Zigzag Level Order Traversal Java

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 retur

Java parseInt()和parseFloat()的用法

一.parseInt()parseInt()方法首先查看位置0处的 字符,判断它是否是个有效数字:如果不是,该方法将返回NaN,不再继续执行其他操作.但如果该字符是有效数字,该方法将查看位置1处的字符,进行同样的 测试.这一过程将持续到发现非有效数字的字符为止,此时parseInt()将把该字符之前的字符串转换成数字.例如如果要把字符串 "1234blue "转换成整数,那么parseInt()将返回1234,因为当它检测到字符b时,就会停止检测过程.parseInt()方法还有基模式