Java – decimal conversion error
I'm writing a program to convert octal numbers to decimals It compiles correctly and everything, but my conversion code has some major errors This seems perfectly logical to me, but anyway, when I run a program conversion error (i.e. 1 is converted to 36), can anyone point out the error?
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 conversion method. My main method is to collect octal;
Solution
This is the problem:
8^7
^The operator didn't do what you thought It does binary XOR
However, I would say that the whole design is obviously suspicious The int value is not an "in" octal or any other Radix - it's just an integer The number 10 is the number 10, whether it is decimal "10", hexadecimal "a" or octal "12" If you have a character sequence to parse into octal digits, the input to this method should be string, not int