Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.LinkedList;
- public class BinaryToDecimalConverter { // only works with small int numbers cuz i'm lazy
- private static int convertBinaryToDecimal(int number) {
- ArrayList<Integer> list = new ArrayList<>(); // a list of separate digits of the number being converted
- LinkedList<Integer> stack = new LinkedList<>(); // a stack used to put the digits onto the list in the right order after separating them
- while (number > 0) { // get separate digits of the number, put them on a stack
- stack.push(number % 10);
- number = number / 10;
- }
- while (!stack.isEmpty()) { // put digits onto the list in correct order
- list.add(stack.pop());
- }
- int decimalNumber = 0;
- int digits = list.size() - 1;
- for (int item : list) { // converts into decimal using base-q expansion representation of the number
- decimalNumber += item << digits;
- digits--;
- }
- return decimalNumber;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement