How to get character code (ASCII and Unicode) in C#

3 Answers

0 votes
using System;

class Program
{
    static void Main() {
        char ch = 'a';

        Console.WriteLine((int)ch);   
    }
}




/*
run:

97

*/

 



answered Jan 20, 2023 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        char ch = 'a';
        
        int char_code = (int)ch;
        
        Console.WriteLine(char_code);   
    }
}




/*
run:

97

*/

 



answered Jan 20, 2023 by avibootz
0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        string s = "你";
        // Unicode
        int code = char.ConvertToUtf32(s, 0);
        Console.WriteLine(code);  
        Console.WriteLine($"U+{code:X}");
    }
}


/*
run:

20320
U+4F60

*/

 



answered Apr 15 by avibootz
...