我正在尝试实现从十进制数到ascii的转换。现在,我已经硬编码为50但我稍后会使用userinput。当我运行应用程序并调用方法时,输出的内容是随机的(每次按下它都会改变)例如“[C @ 4263f600”
我在链接中跟踪了该过程:decimal to binary。为什么我会得到如此奇怪的输出?
//method
if (valid) {
String str = ascii();
mTextOutput.setText(str);
...
public String ascii(){
char[] binary_reverse = new char[9];
char[] binary = new char[9];
int ascii = 50;
int y = 0;
while (ascii !=1) {
if (ascii % 2 ==0)
{
binary_reverse[y]='0';
}
else if (ascii % 2 == 1)
{
binary_reverse[y]='1';
}
ascii /= 2;
y++;
}
if (ascii ==1)
{
binary_reverse[y] = '1';
}
if (y<8) {
for(; y < 8; y++) {
binary_reverse[y] = '0';
}
}
for (int z = 0; z < 8; z++) {
binary[z] = binary_reverse[7-1];
}
String str = binary.toString();
return str;
}
答案 0 :(得分:0)
//你可以试试这个:
binary[z] = binary_reverse[7-1]; - So you want to set every element of binary to the value in binary_reverse[6]
//还有
String str = binary.toString(); to String str = new String(binary);
参考:@Tim