Java八进制到二进制转换(无预定义方法)
我正在尝试创建一个计算器,将八进制值转换为二进制值Java八进制到二进制转换(无预定义方法),java,string,for-loop,Java,String,For Loop,我正在尝试创建一个计算器,将八进制值转换为二进制值 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
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);
temp=txt.getText();
TENTEMP=“”;
如果(上一个==3){
对于(int i=0;i
我的循环语句有什么问题?请帮助。谢谢:)
编辑:
我现在知道这个问题了。谢谢大家的评论和回答。我以1的增量离开了。我应该从
==0开始。对不起,谢谢:)对于你的情况,这是正确的转换。
您可以检查转换
for(int i=0;i
相反,您可以在数字上循环,每次将数字除以2,在循环内通过%2
(或使用>
)检查他的结果。如果是0
,则将数字0
,否则1
,对于您的情况,这是正确的转换。
您可以检查转换
for(int i=0;i
相反,您可以在数字上循环,每次将数字除以2,在循环内通过%2
(或使用>
)检查其结果。如果是0
,则将数字0
,否则1
您在转换表中犯了几个错误。(no 0和no 7,转换错误,通过过于频繁地停止循环来匹配最后一个字符)
在您纠正这些错误后,您应该考虑使用<代码> StringBuilder <代码>,而不只是连接字符串,而Switter语句可能比IF链
更可读。
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();
StringBuilder strBuilder=new StringBuilder();
对于(int i=0;i
还有一种可能性,它的篇幅要短得多(而且完全不可读;):
String[]convArray={“000”、“001”、“010”、“011”、“100”、“101”、“110”、“111”};
StringBuilder strBuild=新建StringBuilder();
对于(int i=0;i
请注意,只有当字符串真正只包含数字0-7时,这才有效。您在转换表中犯了几个错误。(没有0和7,转换错误,通过停止循环来匹配最后一个字符)
在您纠正这些错误后,您应该考虑使用<代码> StringBuilder <代码>,而不只是连接字符串,而Switter语句可能比IF链
更可读。
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();
StringBuilder strBuilder=new StringBuilder();
对于(int i=0;i
还有一种可能性,它的篇幅要短得多(而且完全不可读;):
String[]convArray={“000”、“001”、“010”、“011”、“100”、“101”、“110”、“111”};
StringBuilder strBuild=新建StringBuilder();
对于(int i=0;iString[] 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();
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);
}
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;
}