[Solved] count up to 16 with one digit

I’m working on a game where the player going to encounter a alien race, which is suppose to have a digit up to 16. I have manage to make the code that can translate Alien to Human. But not Human to Alien. I’m using A-G in the code for the 10-16 digits. How do I make the Human to Alien method?

Just in case someone wants/need to see my code that translate Alien to Human.


public static int fromAlienNumber(String nr){
        boolean isNegative = nr.contains("-");
        
        //Code to see if the number is compatible with the alien alphabet.
        Matcher m = Pattern.compile("(-?[0-9A-G]+)").matcher(nr);
        if(!m.matches()){
            System.err.println(nr+" is not an alien number");
            return -1;
        }
        
        int result = 0;
        nr = nr.replace("-","");
        
        for(int i=0; i<nr.length(); i++)           
            result += (int)(charToNumber(nr.charAt(i)) * Math.pow(10, nr.length()-(i+1)));              
        return isNegative ? -result : result;
    }
    
    public static int charToNumber(char c){
        switch(c){
            case '0': return 0;
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return 4;
            case '5': return 5;
            case '6': return 6;
            case '7': return 7;
            case '8': return 8;
            case '9': return 9;
            case 'A': return 10;
            case 'B': return 11;
            case 'C': return 12;
            case 'D': return 13;
            case 'E': return 14;
            case 'F': return 15;
            case 'G': return 16;

        }
        
        //To make the compiler happy, it should never come here.
        return -1;
    }


System.out.println(Integer.parseInt("GGG", 17));
System.out.println(Integer.toString(1111, 17).toUpperCase());

works perfectly fine for me? Why reinvent the wheel?

also your code is wrong:

it will parse GG-G as -GGG not to mention it’s a lot less efficient than Integer.

toAlienNumber -> Integer#toHexString(int)
fromAlienNumber -> Integer#decode(String)
EDIT: Dxu1994 was faster and more exact.

A little more context would be nice.

  1. Because I didn’t know the well existed
  2. That code don’t work.

System.out.println(Integer.parseInt("A0", 17)); //170
System.out.println(fromAlienNumber("A0")); //100

if we had a digit for 10, I don’t think A0 would be 170, since A means 10, and 0 means 0. I have no clew how a 7 got into the mix

This isn’t too hard, you just have to convert from base 10 to base 17 or vice versa (it’s base 17 since you have 17 possible digits). How you do this is successively divide your number by whatever base you are converting to and keep track of the remainders.

This is untested so it might have a typo or something



public static String convertToBase(int input, int base) {

 String output = "";
 int value = input;

 while(value > 0)
 {
   remainder = value % base;
   output = getDigit(value).concat(output);  // have getDigit return a string containing the desired digit for this value and tack it on the front
   value /= base;
 }

 return output;
}


In your case, just call convertToBase(whateverNumebr, 17). That should work fine. Converting from one base to another is a similar process.

EDIT: I didn’t notice that you wanted it to work for negative values as well. Just check if the integer is negative at the beginning of the method and multiply by -1. Then put a final “-” string at the front of output before you return.

2nd EDIT: Oh wow, I didn’t realize that the Integer.parseInt() method could do this for you. I agree with those above, just use that instead of writing your own method. Still, it’s nice to know how it works!

No, 170 is correct, 100 is incorrect since you’re working in base 17.

A = 10, the 0 means carry 17 over.

17 * 10 = 170 not 100.

Here’s how you work it out:

17^3 17^2 17 1
0 0 A (10) 0

17^3 x 0 + 17^2 x 0 + 17 x 10 + 1x0 = 170, not 100.

Similarly in base 10 (decimal):

10^3 10^2 10 1
0 0 10 0

10^3 x 0 + 10^2 x 0 + 10x10 + 1x0 = 100

This is correct. You are not in hexidecimal since you have 17 symbols (0-9 and A-G) representing values. That means that each place value represents a power of 17. In regular old decimal we call these the 1’s place, 10’s place, 100’s place, etc., which you learned about in school. So your alien numerical system works on base 17 so you have the 1’s place, 17’s place, 289’s place (1717), 4913’s place (1717*17), and so on.

Thanks, now I understand how it works. I was so stuck in base 10 that I didn’t know what changing to base 17 was going to do ^^

However, if even you as the developer had problems to understand this, maybe it’s not such a good idea to put it into the game… ???

And your reason behind that is, what exactly? I want to push my boundaries so I can learn something. If I didn’t I wouldn’t been able to code at all since I don’t understand it there for I shouldn’t learn it.

It was just a hint that the player might have the same or worse problems. So if its important to the game to be able to get these numbers, almost nobody will be able to. If it’s not important, then it doesn’t matter…

That’s what play testers are for. But I had just planned to use this as a count down in seconds to things like self-destruct. But also have it spread around the game to clew the players that it’s numbers.

@Yemto,

If you want something a little more interesting you should look into alternate numerical systems. For example, the ancient Mayans had a cool counting system based on base 60(!) and writing a translator could be a lot of fun actually. You could change the symbols to be more “alieny” as well. I like the idea of aliens having their own language in general, I think that’s pretty cool.

Good luck with your game.

Thanks, I think I stay at base 17, at least for this race. But I will use another font so the alien words looks different, I’m to lazy to come up with a whole new language.