Decimal to Octal Conversion [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 12:59:43

The following converts from decimal to octal,

import java.util.Scanner;

public class test { 
    public static void main ( String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number: ");
        int oct  = input.nextInt();
        String result= Integer.toString(oct,8);
        System.out.println(result);
    }
}

The following converts from octal to decimal,

public static void main ( String args[]) {
     Scanner input = new Scanner(System.in);
     System.out.print("Enter number: ");
     String oct  = input.next();
     int result= Integer.parseInt(oct,8);
     System.out.println(result);
 }

The following is a more algorithmic way of converting from octal to decimal,

     public static int convert(String oct) {
         int i= 0;  
         for(int j = 0; j < oct.length(); j++) { 
                char num = oct.charAt(j);           
                num -= '0';     
                if(num<0||num>7) {          
                    sysout("invalid number");
                    return -1;
                }
                i *= 8;                          
                i += num;                      
            }
            return i;
        }
     }

The following is for converting a decimal to octal,

public static int convert(int OctalNumber){
   int counter=0;
   int result = 0;
   while(OctalNumber !=0) {
        int temp = (int) ((OctalNumber%8) * Math.pow(10, counter));
        counter++;
        result += temp;
        OctalNumber /= 8;
    }
    return result;
}

Use the shift functionality. Rather than using <1000 etc.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

First of all, your code converts octal to decimal.

Secondly, you have

if(oct < 1000){

and

if (oct < 100){

but you have no if statement to handle cases in which the input number is greater than 1000.

Edit: I just realized that this really isn't the problem. When you start calculating d2 you need to use the end value of d3 after subtracting 100 however many times. So right before the second if you need a

d2 = d3;

You still need to handle inputs greater than 1000, though.

This helper method may be of help:

int strangeDecimalAsBaseN(int number, int base) {
  if (base < 2 || base > 10) throw new InvalidArgumentException("Impossible or unsupported base " + base);
  if (number < 0) throw new InvalidArgumentException("Negative number (" + number + ") not supported");
  int result = 0;
  int shift = 1;  
  while(number > 0) {
    int lastResult = result;
    result += shift * (number % base);
    if (lastResult > result) throw new IllegalStateException("Overflow!");
    shift *= 10;
    number /= base;
  }
  return result;
}

Or perhaps this, if you actually wanted it the other way:

int strangeFixBaseN(int funnyNumber, int base) {
  if (base < 2 || base > 10) throw new InvalidArgumentException("Impossible base " + base);
  if (funnyNumber < 0) throw new InvalidArgumentException("Negative number (" + funnyNumber + ") not supported");
  int result = 0;
  int shift = 1;  
  while(funnyNumber > 0) {
    int lastResult = result;
    result += shift * (funnyNumber % 10);
    if (lastResult > result) throw new IllegalStateException("Overflow!");
    shift *= base;
    funnyNumber /= 10;
  }
  return result;
}

Beware, untested code :)

You could go with a bit of a better algorithm that allows any decimal number of reasonable length (ie. that will not cause an overflow):

public int octalToDecimal(int octal)
{         
   int numDigits = Integer.toString(octal).length();
   int decimalResult = 0;
   int mask = 0x00000007;
   for(int i = 0; i < numDigits; i++)
   {
      int octalDigit = octal & mask;
      octal = octal >> 3;    
      decimalResult += octalDigit * Math.pow(8, i);
   }

   return decimalResult;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!