0

Apologies for the possibly poorly written question and lack of knowledge.

Im trying to store the actual character "0" in an array but it keeps defaulting to null. Is there anyway to avoid this? Below is a modified version of my code. I am trying to compare two binary numbers and then when the numbers dont match, the following numbers become all zeroes. Any help would be greatly appreciated

        String fullBinOne = "1010101010100101"
        String fullBinTwo = "1010101010101010"
        char[] charBinOne = fullBinOne.toCharArray();
        char[] charBinTwo = fullBinTwo.toCharArray();
        char[] routeSummaryArray = fullBinOne.toCharArray();

        int subnetCIDR = 0;

        for (int i = 0; i < charBinOne.length; i++){
            if (charBinOne[i] != charBinTwo[i]) {
                subnetCIDR = i;
                break;
            }
        }

        for (int i = subnetCIDR; i < charBinOne.length; i++){
            routeSummaryArray[i] = (char)0;
        }

The output of routeSummaryArray is "101010101010 " instead of being 1010101010100000

3
  • 5
    The character 0 is indicated by '0', not by (char) 0. Commented Jul 29, 2020 at 8:05
  • How do you output routeSummaryArray? Commented Jul 29, 2020 at 8:06
  • routeSummaryArray[i] = '0'; Commented Jul 29, 2020 at 13:45

1 Answer 1

1
routeSummaryArray[i] = (char)0;

(char)0 is typecasting the integer value(here 0) to character. Here, the ASCII value of the integer value (here 0) will be stored to the specified index of the character array named "routeSummaryArray".

ASCII value of 0 is null. So it outputs as "101010101010".

If you need output as 1010101010100000, either use

routeSummaryArray[i] = '0';

or,

routeSummaryArray[i] = (char)48;

'0' is the ASCII value of 48".

For your reference, http://www.asciitable.com/

Sign up to request clarification or add additional context in comments.

1 Comment

But more importantly, character '0' has a UNICODE value of 48.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.