#94 – Converting Between Char and Numeric Types

You can implicitly convert from char to any numeric type that can store values in the range [0,65535].  This means that you can convert to int implicitly, as well as ushort, but not to short.

 char c1 = 'X';
 int n1 = c1;     // Implicit conversion to numeric
 ushort s1 = c1;  // Also works
 short s2 = c1;   // Implicit not allowed, must cast
 byte b = c1;     // Implicit not allowed, must cast

You can also convert from a numeric type to a char, but no implicit conversion exists–you must always use a cast or the Convert class.

 short n2 = 90;
 char c2 = (char)n2;  // No implicit conversion, must use cast
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: