Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How is ctrl+A input turned into a smiley?

Tags:

c#

While learning about Console.ReadLine() in depth, I found something very peculiar.

I wrote following code in Main method in a console application in C#

string str = Console.ReadLine();
Console.WriteLine(str);
Console.WriteLine(str.Trim());
Console.ReadLine();

When I ran the code, I gave input as ctrl+A and pressed enter. I see smileys (not able to post image, as I don't have permission to do it yet).

I want to understand, how is it showing the smiley? Shouldn't it show ^A or empty/blank (empty because when I tried debugging it, it showed string value a " ".)

like image 744
Abhishek Avatar asked Mar 18 '23 14:03

Abhishek


2 Answers

If you look at that Encoding used by Console then you can see that it is used IBM437.

Now go to page : http://en.wikipedia.org/wiki/Code_page_437

You will find that 1 has Smiley.

When you press ctrl + A , it will traslated into 1.

So you get that smiley.

        string str = Console.ReadLine();            
        Console.WriteLine((int)str[0]);  // Integer value of character.
        // Console.OutputEncoding ( to get further detail)
        Console.ReadLine();
like image 152
dotnetstep Avatar answered Apr 01 '23 17:04

dotnetstep


The smiley is just how the console displays U+0001 (start of heading) - and that's the character that apparently is read as input from the console when you type Ctrl-A... a bit like the way that if you type Ctrl-G you get U+0007 (bell).

This isn't really .NET behaviour, so much as the Windows console behaviour (both input and output) - I can reproduce exactly the same behaviour in Java, for instance.

like image 44
Jon Skeet Avatar answered Apr 01 '23 17:04

Jon Skeet