【发布时间】:2018-08-18 16:34:41
【问题描述】:
顺便说一句,我是 Stack Exchange 的新手 - 我想我会指出这一点。我在使用按位运算符(无模或除法!!)将十进制数转换为二进制形式的程序时遇到问题,在这种情况下符号无关紧要。问题是,我什至不知道我到目前为止(见下文)是否是第一个 - 我什至无法读取输出!我使用的是 IntelliJ 2017.3,我知道 word 格式不是这种情况。如果有人需要更多信息,我会提供。
代码(更新):
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main
{
public static char[] DecToBinary(int number)
{
// The output array that the binary version of "number" is stored in
char output[] = new char[32];
// The mask used to check if the bytes are all set
int mask = 1;
// The binary array used to store the binary number
char[] binaryString = new char[output.length];
char[] binary = Integer.toBinaryString(number).toCharArray();
// Storing the binary value in the binary string array
for (int i = binary.length - 1; i > 0; i--)
{
binaryString[i] = binary[i];
}
// Looping through the BS array, starting from the end of the array
// (so that the output looks something like this: 01111000 - that's 120 in Binary)
for (int i = 31; i > 0; i--)
{
// Checking if the bytes are all set using the AND operator
output[i] = binaryString[i];
if ((number & mask) == 0)
{
output[i] = '0';
} else if ((number & mask) != 0)
{
output[i] = '1';
}
mask >>>= 1;
}
// Storing the binary string in the output array
for (int i = 32; i > output.length; i--)
{
output[i] = binaryString[i];
}
mask = mask << 1;
// Return the output array
return output;
}
// To be developed...eventually.
public static int BinaryToDec(char number[])
{
int output = 0;
return output;
}
public static void println(String prefix, char array[])
{
System.out.print(prefix);
for (char c : array)
{
System.out.print(c);
}
System.out.println();
}
public static void print(String prefix, char array[])
{
System.out.print(prefix);
for (char c : array)
{
System.out.print(c);
}
}
public static void TestDecToBinary()
{
int valuesToTest[] = { 0, 1, -1, 100, -100, 2147483647, -2147483648 };
System.out.println(" Decimal Binary");
for (int index = 0; index < valuesToTest.length; ++index)
{
System.out.format("%11d ", valuesToTest[index]);
println("", DecToBinary(valuesToTest[index]));
}
}
public static void TestBinToDecimal()
{
char valuesToTest[][] = { { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' }, { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1' }, { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' }, { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '1', '0', '0' }, { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '0', '1', '1', '1', '0', '0' },
{ '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' }, { '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' } };
System.out.println("Binary Decimal");
for (int index = 0; index < valuesToTest.length; ++index)
{
print("", valuesToTest[index]);
System.out.format(" %11d\n", BinaryToDec(valuesToTest[index]));
}
}
public static void main(String[] args)
{
/*File file = new File("myOutput.txt");
FileOutputStream fos;
try
{
fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}*/
TestDecToBinary();
System.out.format("\n\n");
TestBinToDecimal();
}
}
十进制-二进制转换的输出(我必须截屏,因为当我复制它时,它粘贴的只是空格):
【问题讨论】:
-
试试
output[i] = '0',类似'1'。 -
问题是,我应该使用按位运算符。我不能使用其他任何东西,所以...是的。有什么建议吗?
-
仍然没有变化,不幸的是...输出仍然是一堆 X 框...
-
你在哪里返回
output?你是如何打印的?显示您的整个代码。为什么你不能打印字符串binary。这不是你的答案吗? -
要么使用
Integer.toBinaryString方法执行此操作,要么使用您所说的按位操作执行此操作。您试图同时使用两种不同的方法,而您自己却感到困惑。