Program to create a base10 to 7 encoder

Saurabh asked 3 years ago
golibrary

golibrary

How will you create a base 10 to 7 encoder satisfying the below mapping ?

Base 10 0 1 2 3 4 5 6
Base 7 0 a t l s i n

 

Input: 7

Output: a0

 

Input: 7792875

Output: atlassian

Write a program to achieve the same !!

1 Answers
Saurabh answered 3 years ago

Here is a small java program to achieve the same:-  

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Base7Encoder {
static String convert(long input) {
       int base10[] = {0,1,2,3,4,5,6};
       char base7[] = {'0', 'a', 't', 'l', 's', 'i', 'n'};
        long p = 1;
        long base7eq = 0;
        long base = 7;
        long digit = 0;
        while (input != 0) {
           digit = input % base;
           base7eq = base7eq + digit * p;
            p = p * 10;
            input = input / 7;
        }
       

        String b7 = Long.toString(base7eq);
        String output = new String();
        for (int i = 0; i < b7.length(); i++) {
            output += Character.toString(base7[b7.charAt(i)-0x30]);
        }
        return output.toString();
    }
	
	
	public static void main(String args[]) {
		Base7Encoder b = new Base7Encoder();
		System.out.println(b.convert(7));
		System.out.println(b.convert(7792875));
	}
	
}