C
C#2y 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
Angius2y 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
MODiX2y 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
Angius2y 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
Pobiega2y ago
That said, it's an int variable, sonit shouldn't be converting to char
Angius
Angius2y ago
True
MODiX
MODiX2y 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
Angius2y ago
¯\_(ツ)_/¯
Pobiega
Pobiega2y ago
Yuup. Running old compiled version perhaps?
Jibz
JibzOP2y 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
Pobiega2y ago
bruh
Angius
Angius2y ago
So... what's the actual code that doesn't work? Or does everything work now and this thread can be closed?
Pobiega
Pobiega2y ago
They did convert tochar on 3, so eof
Jibz
JibzOP2y ago
yeah. I thought wrongly... close it please
Pobiega
Pobiega2y ago
Do it yourself, with $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Angius
Angius2y ago
So everything works now? Cool
Jibz
JibzOP2y ago
yeah $close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered

Did you find this page helpful?