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