from Base 8 number - Java java.lang

Java examples for java.lang:Number

Description

from Base 8 number

Demo Code


//package com.java2s;

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

    public static int fromBase8(String base8Number) {
        return fromOtherBaseToDecimal(8, base8Number);
    }/*w ww  .  j  av a  2s  .  co  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