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