Click here to Skip to main content
13,699,870 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
While I was converting a integer(int) to char,I got a problem.When I print the character to the ide, the ide shows nothing.Please help me out guys.
I have posted the code below.

What I have tried:

int a = 1;<br />
char c = (char)a;<br />
Console.WriteLine(c);
Posted 11-Jul-18 8:52am
Updated 11-Jul-18 10:16am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Check what the ASCII code of an integer value of 1 means e.g. here:Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]

Your 1 is below integer 32 (Space) which means it is unprintable.

Try e.g. with ...
int a= 65
... and see the difference.It should Show you an "A".

Btw, maybe your confusion?
You "cast" integer 1 to char and expect it will be shown as "1". But no, as you can see in the table mentioned above, the ASCII code for "1" is integer 49.

So try again with...
int a= 49
... and see the difference.It should Show you an "1".

Finally in case you need the string representation for a given integer you can do this:
int a= 1;
string myString = a.ToString();



I hope this helps.
  Permalink  
v5
Comments
Afzaal Ahmad Zeeshan 11-Jul-18 15:43pm
   
5ed.
0x01AA 11-Jul-18 15:46pm
   
Wow, thank you very much.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Quote:
Converting int to char in C#?

Your code is not converting the integer 1 to char '1', it casting integer 1 to char with ascii code 1.
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]
As you can see in ascii table char '1' is ascii code 49.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180910.1 | Last Updated 11 Jul 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100