Checking if ASCII is in two ranges

c#

Solution

(As L.B says, there are simpler ways of doing this - but it's worth examining why your current code doesn't work, too.)

When running the code every returned ASCII value is 32

Yes, it would do. Look closely at your loop:

while (I < ASCIIValues.Length)
{
    {
        if ((ASCIIValues[I] > 65 & ASCIIValues[I] < 90) || (ASCIIValues[I] > 97 & ASCIIValues[I] < 122))
        {

        }
        ASCIIValues[I] = 32;
    }
    Console.WriteLine(ASCIIValues[I]);
    I++;
}

You're doing nothing within the `if` block, and instead you're unconditionally setting `ASCIIValues[I]` to 32.

Additionally:

- I'd use a `for` loop rather than a `while` loop.

- You don't really need to convert the characters to bytes here; each ASCII character has the same Unicode value anyway.

- Specifying the characters using character literals (`'A'` etc) would make the code simpler to read

- You should adopt .NET naming conventions to make your code more idiomatic

- You should use `&&` instead of `&` here

Problem

``` byte[] ASCIIValues = Encoding.ASCII.GetBytes(myInput); while (I < ASCIIValues.Length) { { if ((ASCIIValues[I] > 65 & ASCIIValues[I] < 90) || (ASCIIValues[I] > 97 & ASCIIValues[I] < 122)) { } ASCIIValues[I] = 32; } Console.WriteLine(ASCIIValues[I]); I++; } ``` This is what I have now and I am trying to make sure the string the user enters (my Input) is within the ranges of being a letter only. I am trying to remove all punctuation and special characters as well as numbers. I is equal to 0 and is used to iterate through the array. Changing all unwanted characters to a space is because further down in my code I am removing the spaces anyway. When running the code every returned ASCII value is `32`. This makes no sense as letters should return the corresponding ASCII value.

Original source