struggling
struggling

Reputation: 535

How to convert integer as character in c#?

I have got a situation where i add character to the Observablecollection. The value i have to add is 7 which is of integer type, now when i add it like this:

ObservableCollection<char> numbersList = new ObservableCollection<char>(word);
char Xis = Convert.ToChar(11 - returnModeofEleven(sum));
numbersList.Add(Xis);
Console.WriteLine("char is :" + Convert.ToChar(Xis));

Now nothing is printed on console for Convert.ToChar(Xis) , i want to print 7 as value i got from Convert.ToChar(11 - returnModeofEleven(sum)); is of int type and it's 7.

How to transform this int to char such that my console will print

 char is : 7

instead of (empty)

char : 

On debugging when i see the value at the position where i have added in numbersList then i get numbersList [8] = 7 '\a' , so it while converting int (7) to char Convert() converts it to '\a'(which is 7 of ascii) but i do not want it.I want to convert int of 7 to 7 of char and get saved in my observable collection.

Upvotes: 2

Views: 862

Answers (1)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

Numbers does not start from 0 in ASCII characters. they start from 48. Add value to '0' to get the correct char.

Console.WriteLine("char is :" + (char)('0' + Xis));

Or simply cast Xis to int.

 Console.WriteLine("char is :" + (int)Xis);

Upvotes: 3

Related Questions