joesumbody122
joesumbody122

Reputation: 155

Why is a string printed as a unicode character rather than readable text in C#?

I have a string that I create as a get; set;:

public static string userDirInput { get; set; }

I use Console.Read(); to give it a value:

userDirInput = Convert.ToString(Console.Read());

and before I go to compare it I print it out :

Console.Write("read as " + (string)userDirInput);

Its printing out unicode values and not readable text...

what do I do to get it to print out readable text?

Upvotes: 1

Views: 165

Answers (3)

Hamza_L
Hamza_L

Reputation: 654

You can try Convert.ToChar(Console.Read()).ToString();

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564831

You probably want to use Console.ReadLine, which returns a string. Console.Read only reads a single character, and returns it as an Int32. If you were to cast it to char, you'd see you are reading the first letter the user typed, not the entire string.

Upvotes: 5

Zbigniew
Zbigniew

Reputation: 27614

It's because Console.Read returns an int, so it's more suitable to get char:

int i = Console.Read();
char ch = Convert.ToChar(i);

Use ReadLine to get input as string:

string input = Console.ReadLine();

Upvotes: 2

Related Questions