C
C#16mo ago
Jibz

✅ How do I convert a char to ascii and ascii back to char?

I tried this:
int b = (int)'b';
int e = (int)'e';
int c = e - b;
Console.WriteLine("char is: " + c);
int b = (int)'b';
int e = (int)'e';
int c = e - b;
Console.WriteLine("char is: " + c);
and the output is:
char is:
basically .... nothing
18 Replies
Angius
Angius16mo ago
Because you're trying to display a difference So probably some invalid char What you should do if you're hoping for an actual letter, is add 'a' to it
MODiX
MODiX16mo ago
Angius
REPL Result: Success
'e' - 'b'
'e' - 'b'
Result: int
3
3
Compile: 200.096ms | Execution: 67.586ms | React with ❌ to remove this embed.
Angius
Angius16mo ago
You get a char with ASCII number of 3 So the character for, literally, END OF TEXT: https://www.fileformat.info/info/unicode/char/0003/index.htm
Pobiega
Pobiega16mo ago
That said, it's an int variable, sonit shouldn't be converting to char
Angius
Angius16mo ago
True
MODiX
MODiX16mo ago
Angius
REPL Result: Success
int b = (int)'b';
int e = (int)'e';
int c = e - b;
Console.WriteLine("char is: " + c);
int b = (int)'b';
int e = (int)'e';
int c = e - b;
Console.WriteLine("char is: " + c);
Console Output
char is: 3
char is: 3
Compile: 690.835ms | Execution: 37.820ms | React with ❌ to remove this embed.
Angius
Angius16mo ago
¯\_(ツ)_/¯
Pobiega
Pobiega16mo ago
Yuup. Running old compiled version perhaps?
Jibz
JibzOP16mo ago
yeah i know, sorry about that. First, I had written Convert.ToChar(c) but later I changed it to just see int value but copied the exact code my bad
Pobiega
Pobiega16mo ago
bruh
Angius
Angius16mo ago
So... what's the actual code that doesn't work? Or does everything work now and this thread can be closed?
Pobiega
Pobiega16mo ago
They did convert tochar on 3, so eof
Jibz
JibzOP16mo ago
yeah. I thought wrongly... close it please
Pobiega
Pobiega16mo ago
Do it yourself, with $close
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
Angius
Angius16mo ago
So everything works now? Cool
Jibz
JibzOP16mo ago
yeah $close
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered

Did you find this page helpful?