[英]decimal to binary in java
I'm having trouble in getting the binary. 我在获取二进制文件方面遇到了麻烦。 I do not know what's wrong. 我不知道出了什么问题。 The binary number always ends up in gibberish. 二进制数总是以乱码结尾。 Also some parts like the new int[31]
thing was from HW but I can't get around to make print the actual binary. 还有一些像new int[31]
东西来自HW,但我无法绕过来打印实际的二进制文件。
public class DectoBinary {
public static void main(String[]args) {
Scanner CONSOLE = new Scanner(System.in);
System.out.print("Please enter a nonnegative integer: ");
int value = CONSOLE.nextInt();
while (value < 0) {
System.out.print("number outside range.");
System.out.print
("Please enter a nonnegative interger more than 0: ");
value = CONSOLE.nextInt();
}
int[] intArray = new int[31];
decimalToBinary(value, intArray);
System.out.println(value + "" + intArray);
}
public static int[] decimalToBinary(int value, int[]intArray) {
int i = 0;
while (value != 0) {
if (value % 2 == 1)
intArray[i] = 1;
else
intArray[i] = 0;
value /= 2;
i++;
}
return intArray;
}
}
I think the error is on this line: 我认为错误就在这一行:
System.out.println(value + "" + intArray);
You cannot print an array of integers like this: you should either convert it to string, or write a loop that prints the array digit by digit: 你不能像这样打印一个整数数组:你应该将它转换为字符串,或者写一个逐位打印数组的循环:
for (int i : inrArray) {
System.out.print(intArray[i]);
}
System.out.println();
You do not need to pass in the output array as well: you can create it inside the function. 您也不需要传入输出数组:您可以在函数内创建它。
public static int[] decimalToBinary(int value) {
int count = 1;
int tmp = value;
while (tmp != 0) {
tmp /= 2;
count++;
}
int[] intArray = new int[count];
// Do the conversion here...
return intArray;
}
您可以简单地使用Integer.toBinaryString(int) 。
Actually the is a very simple way to get binary numbers in java using BigInteger 实际上,这是使用BigInteger在java中获取二进制数的一种非常简单的方法
public String dectoBin(int num){
String s = ""+num;
BigInteger bi = new BigInteger(s);
String bin = bi.toString(2);
return bin
}
BigInteger.toString(2)
returns the number stored on the numerical base specified inside the parenthesis. BigInteger.toString(2)
返回存储在括号内指定的数字基数上的数字。 Is a very easy way to get arround this problems. 是一个非常简单的方法来解决这个问题。
System.out.println(value + "" + intArray); System.out.println(value +“”+ intArray); the 'intArray' is a arrays's address, so, if you want to get actual binary you can use Arrays.toString(intArray) 'intArray'是数组的地址,因此,如果你想获得实际的二进制文件,你可以使用Arrays.toString(intArray)
As dasblinkenlight you need to print the array item by item. 作为dasblinkenlight,您需要逐项打印数组。 If you want a nice alternative, you can use a recursive printing of value mod 2 (modulo 2 gives you 1 or 0) 如果你想要一个不错的选择,你可以使用值mod 2的递归打印(modulo 2给你1或0)
/** print directly*/
public static void decimalToBinary(int value) {
if(value > 1){
System.out.print(decimalToBinary(value/2) + "" + (value%2));
/**recursion with implicit cast to string*/
} else {
System.out.print( (value==0)?"":"1");
}
}
It works with any Base 它适用于任何Base
Well actually to print the array, because all the slots in the array are initialized at 0 you need to detect where the first one begins, so. 实际上打印数组,因为数组中的所有插槽都初始化为0,您需要检测第一个插槽的开始位置,所以。 you need to replace 你需要更换
System.out.println(value + "" + intArray);
with something like this; 有这样的事情;
System.out.println(vale + " ");
boolean sw = false;
for(int i=0;i<intArray.length;i++){
if(!sw)
sw = (intArray[i]==1);//This will detect when the first 1 appears
if(sw)
System.out.println(intArray[1]); //This will print when the sw changes to true everything that comes after
}
Here is a program to convert Decimal nos. 这是一个转换十进制数的程序。 into Binary. 进入二进制。
import java.util.Scanner;
public class decimalToBinary {
static int howManyTerms (int n) {
int term = 0;
while (n != 0) {
term ++;
n /= 2;
}
return term;
}
static String revArrayofBin2Str (int[] Array) {
String ret = "";
for (int i = Array.length-1; i >= 0; i--)
ret += Integer.toString(Array[i]);
return ret;
}
public static void main (String[] args) {
Scanner sc=new Scanner (System.in);
System.out.print ("Enter any no.: ");
int num = sc.nextInt();
int[] bin = new int[howManyTerms (num)];
int dup = num, el = -1;
while (dup != 0) {
int rem = dup % 2;
bin [++el] = rem;
dup /= 2;
}
String d2b = revArrayofBin2Str(bin);
System.out.println("Binary of " + num + " is: " + d2b);
}
}
This is simple java code for decimal to binary using only primitive type int, hopefully it should help beginners. 这是十进制到二进制的简单java代码,只使用原始类型int,希望它应该有助于初学者。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DtoB {
public static void main(String[] args) {
try { // for Exception handling of taking input from user.
System.out.println("Please enter a number");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
int x = Integer.parseInt(input);
int bin = 0;
int p = 1;
while (x > 0) {
int r = x % 2;
bin = (r * p) + bin;
x = x / 2;
p *= 10;
}
System.out.println("Binary of " + input + " is = " + bin);
} catch (Exception e) {
System.out.println("Please enter a valid decimal number.");
System.exit(1);
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.