Question

I am attempting to implement a conversion from a decimal number to ascii. Right now, i've hard coded it to 50 but I will use userinput later on. When I run the application and call the method, what is outputed is random (it changes everytime I press it) eg "[C@4263f600"

I followed the process in the link: decimal to binary. Why am I getting such a weird output?

//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;
}
Was it helpful?

Solution

//You can try this :

binary[z] = binary_reverse[7-1]; - So you want to set every element of binary to the value in binary_reverse[6]

//and also

 String str = binary.toString(); to String str = new String(binary);

reference : @Tim

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top