Letters in base-conversion

Posted by tech_geek23 on Stack Overflow See other posts from Stack Overflow or by tech_geek23
Published on 2012-11-01T22:59:14Z Indexed on 2012/11/01 22:59 UTC
Read the original article Hit count: 597

Filed under:
|
|

I have this code written so far and is correct, aside from not using A-F when the value is over 10:

  public class TenToAny
{
private int base10;
private int newBase;

public TenToAny()
{

}

public TenToAny(int ten, int base)
{
    base10 = ten;
    newBase = base;
}

public void setNums(int ten, int base)
{
    base10 = ten;
    newBase = base;
}
public String getNewNum()
{
    String newNum="";
    int orig = base10;
    //int first = newBase - 1;
    while(orig > 0)
    {
        newNum = orig%newBase + newNum;
        orig = orig/newBase;
    }
    return newNum;
}

public String toString()
{
    String complete = base10 + " base 10 is " + getNewNum() + " in base " + newBase;

    return complete;
}

}

Obviously I don't have anything relating to values over 10 converting to A-F as I've never dealt with these before. Any help is appreciated.

Here's my runner class:

public class Lab09i
{
  public static void main( String args[] )
{
    TenToAny test = new TenToAny(234, 9);
    out.println(test);

    test.setNums(100, 2);
    out.println(test);

    test.setNums(10, 2);
    out.println(test);

    test.setNums(15, 2);
    out.println(test);

    test.setNums(256, 2);
    out.println(test);

    test.setNums(100, 8);
    out.println(test);

    test.setNums(250, 16);
    out.println(test);

    test.setNums(56, 11);
    out.println(test);

    test.setNums(89, 5);
    out.println(test);

    test.setNums(23, 3);
    out.println(test);

    test.setNums(50, 5);
    out.println(test);

    test.setNums(55, 6);
    out.println(test);

    test.setNums(2500, 6);
    out.println(test);

    test.setNums(2500, 13);
    out.println(test);
}
}

this is what my results should be:

234 base 10 is 280 in base 9

100 base 10 is 1100100 in base 2

10 base 10 is 1010 in base 2

15 base 10 is 1111 in base 2

256 base 10 is 100000000 in base 2

100 base 10 is 144 in base 8

250 base 10 is FA in base 16

56 base 10 is 51 in base 11

89 base 10 is 324 in base 5

23 base 10 is 212 in base 3

50 base 10 is 302 in base 4

55 base 10 is 131 in base 6

2500 base 10 is 9C4 in base 16

2500 base 10 is 11A4 in base 13

© Stack Overflow or respective owner

Related posts about binary

Related posts about base