1

OK so I don't get yelled at I did search the forums and there is a very similar question, I'm just having a little difficulty understanding what is meant.

I'm generating a random number in decimal, checking to see if it matches a certain exclusion range for characters I don't want to print, if it does not match the exclusion range, I save the int and I want to send it to the char to be printed.

I don't know how to tell visual studio to print the decimal integer 65 as the char "A" heres my code:

int asciVal = rnd.Next(33, 96);
if (asciVal == 48 || asciVal == 49 || asciVal == 50 || asciVal == 51 || asciVal == 52 || asciVal == 53 || asciVal == 54 || asciVal == 55 || asciVal == 56 || asciVal == 57 || asciVal == 65 || asciVal == 66 || asciVal == 67 || asciVal == 68 || asciVal == 69 || asciVal == 70 || asciVal == 71 || asciVal == 72 || asciVal == 73 || asciVal == 74 || asciVal == 75 || asciVal == 76 || asciVal == 77 || asciVal == 78 || asciVal == 79 || asciVal == 80 || asciVal == 81 || asciVal == 82 || asciVal == 83 || asciVal == 84 || asciVal == 85 || asciVal == 86 || asciVal == 87 || asciVal == 88 || asciVal == 89 || asciVal == 90)
    loop = 1;
else {
    loop = 0;
    Text = asciVal;
}
3
  • do you have a link to the other question? Commented May 2, 2016 at 18:35
  • You can shorten your if statement: if ((asciVal >= 48 && asciVal <= 57) || (asciVal >= 65 && asciVal <= 90)). Much more readable than a long line of or statements. Commented May 2, 2016 at 19:43
  • Several answers and comments show how to convert an integer to a char (Unicode/UTF-16 code unit). They are correct because you aren't actually using ASCII. Take what you know about ASCII and port it to Unicode, learn a bit more and then forget ASCII. Commented May 3, 2016 at 1:36

3 Answers 3

5

Use Convert.ToChar

int number = 65;
char c = Convert.ToChar(number); 
Sign up to request clarification or add additional context in comments.

1 Comment

and here's the documentation
1

The simplest way to convert an ascii int to char is the following:

int i = 123;
char c = (char)i;

Comments

1

You can use the unicode in c#.

int asciival= <value>;
char letter = (char) asciival;
string word = letter.ToString();

Comments

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.