from Base 2 number - Java java.lang

Java examples for java.lang:Number

Description

from Base 2 number

Demo Code


//package com.java2s;

public class Main {
    private static final String baseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static int fromBase2(String base2Number) {
        return fromOtherBaseToDecimal(2, base2Number);
    }/*from w w  w  .jav  a2s. c  o m*/

    private static int fromOtherBaseToDecimal(int base, String number) {
        int iterator = number.length();
        int returnValue = 0;
        int multiplier = 1;

        while (iterator > 0) {
            returnValue = returnValue
                    + (baseDigits.indexOf(number.substring(iterator - 1,
                            iterator)) * multiplier);
            multiplier = multiplier * base;
            --iterator;
        }
        return returnValue;
    }
}

Related Tutorials