Convert a number from base 10 to N

This code is part of a project that I have to develop. I need to write a java method that converts a number (base 10) to another number in the database n. This is the class code:

public class converter {

    public int from_10_to_n(int base, int newbase) {

       int k = 0;
       String res = "";
       String output = "";

       do {                        //Below I explain what I'm doing here
        base /= newbase;   
        k = base % newbase;
        res += String.valueOf(k);
       } while (base != 0);

       for(int i=res.length()-1; i>-1; i--) {
        output += res.charAt(i);
       }

       return Integer.parseInt(output);
    }

I thought to make the program this way:

enter image description here

The cycle do {} while();divides the numbers and stores the remainders in k. Then I create a for loop that changes the res line (which has reminders).

By the way, when I call the method in my main, I miss the last digit. I mean:

converter k = new converter();
int e = k.from_10_to_n(a /*base 10 number*/, b /*Base n number*/);

System.out.println(a+" -> "+b+" is " + e);

With this code, if I want to convert 231 to base 4, I get 321as a result instead 3213. I checked my code, but I can not find a solution. Any idea?

. , 31 ( 10) 11111 ( 2), 1111.

+4
3

; , . .

+6

:

base /= newbase;
k = base % newbase;

, 231 4 .

base /= newbase

base 57, k . , :

k = base % newbase;
base /= newbase;

, :

  • base , - , input - ?
  • . , - .
+3

The alphabet of our decimal system is "0123456789". Now the alphabet of an arbitrary alphanumeric system in the base N, large, is "# 0 # 1 # 2 # ... # 9 # 10 # 11 # ... # 1234 # ... # N" So, to summarize, simplify By adding legiblility to the output and doing tests, we get:

/*
 * Program  FromBase10ToBaseN
 * This program receives as input 
 * a positive integer number in base 10
 * and outputs its encoding in arbitrary base N.
 * The k-th symbol of the new base is denoted   #k.
 * Tests are provided.

 * Jose (2016)
 * evoljava.com 

 */


package Programs;

import java.util.Random;

public class FromBase10ToBaseN {


    private static final Random RANDOM = new Random();

    public static void main(String[] args) throws Exception {

        //Number in base 10
        int inputValue = 9;
        //New base
        int N = 10;
        reportFromBase10ToBaseN(inputValue, N);

        inputValue = 100;
        N = 10;
        reportFromBase10ToBaseN(inputValue, N);

        inputValue = 8;
        //New base
        N = 2;
        reportFromBase10ToBaseN(inputValue, N);

        inputValue = 129;
        //New base
        N = 128;
        reportFromBase10ToBaseN(inputValue, N);

        inputValue = 127;
        //New base
        N = 128;
        reportFromBase10ToBaseN(inputValue, N);



        //test with random numbers
        for (int i = 0; i < 10; i++) {
            inputValue = RANDOM.nextInt(1000);
            //New base must be 2 or greater
            N = 2 + RANDOM.nextInt(80);
            reportFromBase10ToBaseN(inputValue, N);
        }

    }


    private static void reportFromBase10ToBaseN(int inputValue, int N) {

        String outputValue = fromBase10ToBaseN(inputValue, N);
        int Backwards = fromBaseNToBase10(outputValue, N);
        String isRight = "wrong";
        if (inputValue == Backwards) isRight = "right";
        System.out.println(inputValue + " in base 10 becomes "
                + outputValue
                + " in base " + N
                + " Backwards: " + Backwards
                + "   " + isRight);
    }


    private static String fromBase10ToBaseN(int inputValue, int N) {
        String res ;
        String outputValue = "";

        do {

            res = "#" + String.valueOf(inputValue % N);
            outputValue = res + outputValue;
            inputValue /= N;

        } while (inputValue != 0);

        return outputValue;
    }



    //The input string denotes a number in base newBase
    //The k-th symbol of the new base is denoted  #k.
    //Example:  #10#0 in base 15  represents 150 in base 10
    private static int fromBaseNToBase10(String inputValue, int N) {

        if (inputValue.charAt(0) == '#') {
            int outputValue = 0;
            boolean correct = true;
            int length = inputValue.length();
            String token = "";
            int power = 0;
            //Tokenizing:
            //Exmpl:  #1#120#13#2 is separated into tokens  1 120 13 2
            for (int i = length - 1; (i > -1) & correct; i--) {
                char c = inputValue.charAt(i);
                if (c != '#') {
                    token = c + token;
                } else {
                    //tokens are evaluated
                    int p = Integer.parseInt(token);
                    //Detecting the presence of an alien symbol
                    if (p >= 0) {
                        outputValue 
                            = (int) (outputValue + p * Math.pow(N, power));
                        power++;
                    } else {
                        outputValue = -1;                      
                        correct = false;
                    }
                    token = "";
                }
            }

            return outputValue;
        } else  return -1;

    }
}//End of Program FromBase10ToBaseN


OUTPUT:

9 in base 10 becomes #9 in base 10 Backwards: 9   right
100 in base 10 becomes #1#0#0 in base 10 Backwards: 100   right
8 in base 10 becomes #1#0#0#0 in base 2 Backwards: 8   right
129 in base 10 becomes #1#1 in base 128 Backwards: 129   right
127 in base 10 becomes #127 in base 128 Backwards: 127   right
382 in base 10 becomes #6#40 in base 57 Backwards: 382   right
390 in base 10 becomes #11#5 in base 35 Backwards: 390   right
788 in base 10 becomes #3#1#4 in base 16 Backwards: 788   right
285 in base 10 becomes #3#4#6 in base 9 Backwards: 285   right
68 in base 10 becomes #1#31 in base 37 Backwards: 68   right
656 in base 10 becomes #24#8 in base 27 Backwards: 656   right
442 in base 10 becomes #9#28 in base 46 Backwards: 442   right
765 in base 10 becomes #21#30 in base 35 Backwards: 765   right
455 in base 10 becomes #10#35 in base 42 Backwards: 455   right
211 in base 10 becomes #4#3 in base 52 Backwards: 211   right
0
source

Source: https://habr.com/ru/post/1547085/


All Articles