简体   繁体   中英

How to convert ascii integer to char in C#?

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;
}

Use Convert.ToChar

int number = 65;
char c = Convert.ToChar(number); 

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

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

You can use the unicode in c#.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM