3

I am writing a program that will convert octal numbers to decimals. It compiles right and everything but there is something majorily wrong with my conversion code. It seems perfectly logic to me, however somehow when I run the program the conversions are wrong (i.e. 1 is converted to 36) can someone point out what is going wrong?

 public static int convert(int octal)
{
    int d1=0,d2=0,d3=0,d4=0,d5=0,d6=0,d7=0,d8=0;

    if(octal >=9999999){
    d8 = (octal-(octal%10000000));}
    if(octal >=999999){
    d7 = (octal-(octal%1000000));}
    if(octal >=99999){
    d6 = (octal-(octal%100000));}
    if(octal >=9999){
    d5 = (octal-(octal%10000));}
    if(octal >= 999){
    d4 = (octal-(octal%1000));}
    if(octal >= 99){
    d3 = (octal-(octal%100));}
    if(octal >= 9){
    d2 = (octal-(octal%10));}
    if(octal >= 0){
    d1 = (octal-(octal%1));}


    octal = (d8 * 8^7) + (d7 * 8^6) + (d6 * 8^5) + (d5 * 8^4) + (d4 * 8^3) + (d3 * 8^2) + (d2 * 8^1) + (d1 * 8^0);

    return octal;

}

this is just my convert method, my main method is what collects the int octal;

Charles
  • 50,943
  • 13
  • 104
  • 142
user1714873
  • 135
  • 3
  • 3
  • 10

4 Answers4

7

This is the problem:

8^7

The ^ operator doesn't do what you think it does. It does binary XOR...

However, I'd say that the whole design is distinctly suspect. An int value isn't "in" octal or any other base - it's just an integer. The number ten is the number ten, whether that's exressed as "10" in decimal, "A" in hex or "12" in octal. If you've got a sequence of characters which you want to parse as octal digits, the input to the method should be a String, not an int.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks for pointing that out, i'm new to java. and frankly this i still got a lot of things to learn. However as for your adivce to use a String, The assignment was to take an int and return and int. so I don't think I am allow to do what you are telling me to do. – user1714873 Oct 30 '12 at 16:36
  • @user1714873: Aside from anything else, you should complain to your tutor then - this is an example of abusing types. – Jon Skeet Oct 30 '12 at 19:41
4

Since your method accepts an integer (commonly represented in a String representation as decimal), and what you're outputting is also an integer, you've not really turned into "an octal integer", you've changed it into some other integer completely. You're trying to correctly convert your integer into an octal, and then incorrectly interpreting that octal as a decimal.

If wish to convert an integer into it's octal string representation, you could simply use the following method:

public static String convert(int number) {
    Integer.toOctalString(number);
}

And, if you truly want to return an int that represents that octal String parsed as if it was decimal, you could simply do this:

public static int convert(int number) {
    return Integer.parseInt(Integer.toOctalString(number));
}
Joel Westberg
  • 2,656
  • 1
  • 21
  • 27
1

If you have repetitive code, you might consider a loop.

public static void main(String... args) {
    for (long i = 7, j = 7; i > 0; i = i * 10 + 1, j = j * 8 + 1) {
        long c = convert(i);
        if (c != j)
            throw new AssertionError(i + ": " + c + " != " + j);
        System.out.println(i + " = > " + j);
    }
}

public static long convert(long octal) {
    long ret = 0;
    for (long i = 1000000000000000000L, j = 1L << (3 * 18); i > 0; i /= 10, j >>= 3)
        ret += j * (octal / i % 10);
    return ret;
}

prints

7 = > 7
71 = > 57
711 = > 457
7111 = > 3657
71111 = > 29257
711111 = > 234057
7111111 = > 1872457
71111111 = > 14979657
711111111 = > 119837257
7111111111 = > 958698057
71111111111 = > 7669584457
711111111111 = > 61356675657
7111111111111 = > 490853405257
71111111111111 = > 3926827242057
711111111111111 = > 31414617936457
7111111111111111 = > 251316943491657
71111111111111111 = > 2010535547933257
711111111111111111 = > 16084284383466057
7111111111111111111 = > 128674275067728457
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You have an underlying misconception that's going to keep messing you up.

There is no such thing as an "octal number", per se. An int isn't decimal, or roman numerals, or octal - it's just a number.

An "octal number" really means "a string containing the octal representation of a number".

Ed Staub
  • 15,480
  • 3
  • 61
  • 91