Answers for "convert base 10 to any base java"

0

convert base 10 to any base java

int input= 220;
int outputBase= 2;
int output=Integer.toString(input, outputBase);//output= base 2 of 220(10) = 11011100
//or
static String convertDecToAny(int outputBase, int intput){
        String result="";
        while(output!=0){
            int value= intput%outputBase;
            intput/=outputBase;
            if(value<=9)
                result= Integer.toString(value)+result;
            else
                result= Character.toString((char)(value+55))+result;//ASCII
        }
        return result;
Posted by: Guest on October-24-2021
0

convert any base to base 10 java

String input="54C";
int inputBase=16;
int output= Integer.parseInt(input, inputBase);//output= base 10 of 54C = 1356
//or more specific way to show off
static int convertToDec(int inputBase, String input){
        int output = 0, index = 0;
        int value = 0;
        input = input.toUpperCase();
        for (int i = input.length() - 1; i >= 0; i--) {
            char c = input.charAt(i);
            if (c >= '0' && c <= '9')
                value = c-48;//in ASCII char '0' is at index 48 so -48 give actual int value  
            else if (c >= 'A' && c <= 'F')
                value = Integer.parseInt(String.valueOf(c)); //or just use String.valueOf(char) instead of -index(ASCII) 
            output += value * Math.pow(inputBase, index);
            index++;
        }
        return output;
    }
Posted by: Guest on October-24-2021

Code answers related to "convert base 10 to any base java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language