[英]Java Octal to Binary Conversion (without predefined methods)
I'm trying to create a calculator that converts an octal value to binary. 我正在尝试创建一个将八进制值转换为二进制值的计算器。
temp = txt.getText();
temptemp = "";
if (prev == 3) {
for (int i = 0; i < temp.length() - 1; i++) {
if (temp.charAt(i) == '1') {
temptemp = temptemp + "000";
} else if (temp.charAt(i) == '2') {
temptemp = temptemp + "001";
} else if (temp.charAt(i) == '3') {
temptemp = temptemp + "010";
} else if (temp.charAt(i) == '4') {
temptemp = temptemp + "011";
} else if (temp.charAt(i) == '5') {
temptemp = temptemp + "100";
} else if (temp.charAt(i) == '6') {
temptemp = temptemp + "101";
} else if (temp.charAt(i) == '7') {
temptemp = temptemp + "111";
}
}
temp = temptemp;
txt.setText(temp);
What's wrong with my looping statement? 我的循环语句出了什么问题? Please help.
请帮忙。 Thank you :)
谢谢 :)
EDIT: 编辑:
I know now the problem. 我现在知道了问题。 Thanks for all the comments and answers guys.
谢谢大家的评论和回答。 I was off by 1 increment.
我离开了1个增量。 I should've start at
== 0
. 我应该从
== 0
开始。 Sorry and thank you :) 抱歉,谢谢:)
You've done several mistakes in your conversion table. 您在转换表中犯了几个错误。 (no 0 and no 7, wrong conversions, ommitting the last character by stopping the loop too eraly).
(没有0和7,错误的转换,由于太过分地停止循环而忽略了最后一个字符)。
After you corrected those you should consider using a StringBuilder
instead of just concatenating strings! 纠正这些错误之后,您应该考虑使用
StringBuilder
而不是仅连接字符串! And a switch statement might be a bit more readable than an if-else chain 而且switch语句可能比if-else链更具可读性
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < temp.length(); i++) {
switch(temp.charAt(i)) {
case '0':
strBuilder.append("000");
break;
case '1':
strBuilder.append("001");
break;
case '2':
strBuilder.append("010");
break;
case '3':
strBuilder.append("011");
break;
case '4':
strBuilder.append("100");
break;
case '5':
strBuilder.append("101");
break;
case '6':
strBuilder.append("110");
break;
case '7':
strBuilder.append("111");
break;
}
}
String temptemp = strBuilder.toString();
There's another possibility which is much shorter (and completely unreadable ;) ): 还有另一种可能性更短(并且完全不可读;)):
String[] convArray = { "000", "001", "010", "011", "100", "101", "110", "111" };
StringBuilder strBuild = new StringBuilder();
for (int i = 0; i < temp.length(); i++)
strBuild.append(convArray[temp.charAt(i)-48]);
String temptemp = strBuild.toString();
Please not that this will only work if the String really only contains the numbers 0-7 请注意,这仅在字符串实际上仅包含数字0-7时才有效
Its easy to do it without considering all 8 branches 无需考虑所有8个分支机构即可轻松完成
for (int i = 0; i < temp.length(); i++) {
int d = Character.getNumericValue(temp.charAt(i));
for (int k=2; k >= 0; k--)
temptemp = temptemp + Character.forDigit((i >> k) & 1, 2);
}
You can skip having this big long list of explicit cases by doing some bitwise operations: 您可以通过执行一些按位操作来跳过大量的显式情况列表:
StringBuilder sb = new StringBuilder(3 * temp.length());
for (int i = 0; i < temp.length(); i++) {
// + check that the character is actually an octal digit.
int digit = Character.digit(temp.charAt(i), 10);
for (int b = 2; b >= 0; --b) {
sb.append(Character.forDigit((digit >> b) & 0x1, 10));
}
}
private static String octalToBinary(int octal) {
if (octal < 1 || octal > 7) {
throw new IllegalArgumentException("Not an ocatl number");
}
String binary = new String();
int myOctal = octal / 2;
for (; myOctal >= 1;) {
binary = octal % 2 + binary;
octal = myOctal;
myOctal = myOctal / 2;
}
binary = octal + binary;
// if you want to make it of length 3
if (binary.length() == 1) {
binary = "00" + binary;
}
if (binary.length() == 2) {
binary = "0" + binary;
}
return binary;
}
For your case it's the right converts. 对于您的情况,这是正确的转换。 You can check the converts here
您可以在此处查看转换
for (int i = 0; i < temp.length() - 1; i++) {
if (temp.charAt(i) == '0') {
temptemp = temptemp + "000";
} else if (temp.charAt(i) == '1') {
temptemp = temptemp + "001";
} else if (temp.charAt(i) == '2') {
temptemp = temptemp + "010";
} else if (temp.charAt(i) == '3') {
temptemp = temptemp + "011";
} else if (temp.charAt(i) == '4') {
temptemp = temptemp + "100";
} else if (temp.charAt(i) == '5') {
temptemp = temptemp + "101";
} else if (temp.charAt(i) == '6') {
temptemp = temptemp + "110";
} else if (temp.charAt(i) == '7') {
temptemp = temptemp + "111";
}
}
Instead, you can loop over the number, each time div the number by 2, inside the loop check his result by % 2
(or by using >>
) if it's 0
, you add to result the digit 0
, else 1
取而代之的是,您可以循环遍历数字,每次将数字除以2时,就在循环内将其结果按
% 2
(或使用>>
)检查其结果是否为0
,然后将结果加到数字0
,否则返回1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.