2012-10-30 137 views
4

我想将十进制数转换为二进制数。我想创建一个将十进制数转换为二进制的方法,然后从Main调用该方法来打印结果,但是我遇到了一些麻烦。这是迄今为止的代码。 toBin方法工作不正常,我似乎无法弄清楚我做错了什么。将十进制数转换为二进制数

我想自己学习java,所以任何帮助将不胜感激,包括解释如果有必要。我通过http://knowledgeblackbelt.com学习JAVA,这是练习之一。就像我说的,我不需要比我想要的例子和解释过程更多的答案,因为我似乎没有弄明白。感谢真正帮助的人。

public class DecToBin { 


public static void main(String[] args) { 

    System.out.println(toBin(2)); 

} 


public static int toBin(int Number){ 


    int result = 0; 
    while(Number > 0){ 


     int mod = Number % 2; 
     result = result * 1 + mod; 

     Number /= 2; 
    } 

    return result; 

} 

}

+0

你有没有使用Google?例如,快速搜索可以提供http://forum.codecall.net/topic/54004-decimal-to-binary-number/。 – MoRe

+0

您正试图将二进制数存储为int,并将其加1,结果为2(十进制)。您可能想要以不同的方式存储结果编号... – MoRe

+2

此处没有小数。一切都已经是二元的。不是一个真正的问题。 – EJP

回答

9

希望这有助于:

/** 
* 
* @author X 
* 
*/ 
public class ConvertDecimal { 

    public void convert(int decimal , int base) 
    { 
     int result = 0; 
     int multiplier = 1; 

      while(decimal > 0) 
      { 
       int residue = decimal % base; 
       decimal  = decimal/base; 
       result  = result + residue * multiplier; 
       multiplier = multiplier * 10; 
      } 

      System.out.println ("binary....." + result); 
    } 



    public static void main(String args[]) 
    { 
     ConvertDecimal conv = new ConvertDecimal(); 
     conv.convert(128, 2); 
    } 

} 
2

它看起来像你的问题是您要添加的+ 1,导致每次将一个数字添加到bin值的时间。我可能会连接一个字符串,然后解析回一个数字。是这样的:

string result = ""; 
    while(Number > 0){ 


     int mod = Number % 2; 
     result = result + mod; 

     Number /= 2; 
    } 
+0

如果我没有错误result = result + mod应该是result = mod +结果,因为我们把余数按相反的顺序排列 – aa1992

10

使用Integer.toString(int number, int radix)

Integer.toString(2, 2); // will return "10" 
Integer.toString(2, 10); // will return "2" 
Integer.toString(15, 16); // will return "F" 
+1

我认为这是一个学校任务,所以我认为他正在寻找一种算法来实现这一点,而不是一个库方法。 –

+0

没想到。那么,至少现在他知道这存在:P –

1

好SOF的想法是帮助人们思考,不解决他们的家庭作业,所以我将描述这个代码,我第C,您就可以翻译如果你愿意,它可以转化为java,重要的是你会理解算法的工作原理。

1.-我声明一些变量i(计数器),base(新基2-16),word[ 32 ](具有32个元件(0-31)的阵列在这里我们将存储新号码)和number(用户想要转换的号码)。

2:我输入numberbase值。

3.-我调用该函数wordCalculator(在这里,我计算的新号码)这个函数有3个参数(文字,数字和底座),该函数的原型,你可以看到,我用一个指针字作为真正的参数(因为我会编辑数组的值)。

3.1.-在这个函数中(这可以是类 - 方法在Java),我声明3个新的本地变量:result(这需要数的值),i(计数器),和difference(这将在操作中使用),但转换是如何工作的?

/* Example: 
    125(10) -----> ?(2)      125 |_2 
              -1- 62 |_2 
                -0- 31 |_2 
                 -1- 15 |_2 
                  -1- 7 |_2 
                   -1- 3 |_2 
                    -1- 1 */ 

所以在本例为125(10),该二进制数是1111101(2),这是我在函数描述该过程。

3.2.-我计算整个分裂%,并将其存储在difference,然后我由base(它的数目的拷贝)划分result并加载它本身(即链接到下一步骤),该操作后余存储的差在阵列字[](这是由指针*(word + i)引用的第一位置,i表示偏移),如果结果给出比基座的操作下一个数完成并且最后一位数字存储在阵列中(这是if (result < base))。

/* Functions declaration (Prototype) */ 

int wordCalculator(int * const word, long int number, int base); 
    int main(void) 
     { 
      int i, base; 
      int word[ 32 ]; 
      unsigned long int number; 

      printf("Enter the decimal number to be converted: "); 
      scanf("%ld", &number); 
      printf("\nEnter the new base: "); 
      scanf("%d", &base); 

      i = wordCalculator(word, number, base); 

      printf("The number is: "); 

      for(; i >= 0; i--){ 

       if (word[ i ] <= 9) 
        printf("%d", word[ i ]); 

       else 
        /* 65 represents A in ASCII code. */ 
        printf("%c", (65 - 10 + word[ i ])); 
      } 

      printf("\n"); 
     } 

     int wordCalculator(int * const word, long int number, int base) 
     { 
      unsigned long int result = number; 
      int i, difference; 

      i = 0; 
      do{ 
       difference = result % base; 
       result /= base; 
       *(word + i) = difference; 
       i++; 

       if (result < base) 
        *(word + i) = result; 

      } while(result >= base); 

      return i; 

     } 
相关问题