Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,646 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,123 people online right now. Registration is fast and FREE... Join Now!




Reading and assigning single characters to char

 
Reply to this topicStart new topic

Reading and assigning single characters to char

Zeddicus
post 16 Aug, 2008 - 11:57 AM
Post #1


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


Is it possible to read a single character from the keyboard and then assign the inserted character to a char variable in a simple way? Which method should you call to do this?
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 16 Aug, 2008 - 12:49 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Define simple, there are many ways to accomplish the task you are asking about.

What have you tried, so far?
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 16 Aug, 2008 - 02:13 PM
Post #3


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


QUOTE(jayman9 @ 16 Aug, 2008 - 01:49 PM) *

Define simple, there are many ways to accomplish the task you are asking about.

What have you tried, so far?


To define simple I must refer to C++ (I really hope it's OK because I am trying to learn these languages at the same time). In C++ you only use cin to store a character in a char variable, then implicitly typecast the char to an int.

The version I am using right now in C# is too complex in my opinion:

CODE
Console.WriteLine("Please enter a character:");
            string input = Console.ReadLine();
            uint i;

            foreach (char c in input)
            {
                i = Convert.ToUInt32(c);
                System.Console.WriteLine("{0}'s ASCII integer value is {1}.", c, i);
            }



First of all, it utilizes a string that is "broken down" into its char components which are then converted to integers. I am looking for a procedure similar to the one described above in C++.

This post has been edited by Zeddicus: 16 Aug, 2008 - 02:18 PM
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 16 Aug, 2008 - 02:27 PM
Post #4


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


QUOTE(Zeddicus @ 16 Aug, 2008 - 06:13 PM) *

QUOTE(jayman9 @ 16 Aug, 2008 - 01:49 PM) *

Define simple, there are many ways to accomplish the task you are asking about.

What have you tried, so far?


To define simple I must refer to C++ (I really hope it's OK because I am trying to learn these languages at the same time). In C++ you only use cin to store a character in a char variable, then implicitly typecast the char to an int.

The version I am using right now in C# is too complex in my opinion:

CODE
Console.WriteLine("Please enter a character:");
            string input = Console.ReadLine();
            uint i;

            foreach (char c in input)
            {
                i = Convert.ToUInt32(c);
                System.Console.WriteLine("{0}'s ASCII integer value is {1}.", c, i);
            }



First of all, it utilizes a string which is then "broken down" into its char components and converted to integers. I am looking for a procedure similar to the one described above in C++.


how about this?

csharp

Console.WriteLine("Please enter a character:");
uint charInt = 0;

while (charInt != 13)
{
charInt = (uint)Console.Read();
System.Console.WriteLine("{0}'s ASCII integer value is {1}.", (char)charInt, charInt);
}



This post has been edited by eclipsed4utoo: 16 Aug, 2008 - 02:34 PM
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 16 Aug, 2008 - 03:07 PM
Post #5


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


In addition to the Read() function, mentioned by eclipsed4utoo, there is also a ReadKey() function which serves just this purpose.

Console.ReadKey()
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 1 Sep, 2008 - 07:36 AM
Post #6


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


jayman9: I'm getting weird results when using the Console.ReadKey method for this task. Should you use it in a special way in order to assign the value to a char? And if I do a explicit typecast like (char) Console.ReadKey, the application throws an exception.
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 1 Sep, 2008 - 08:35 AM
Post #7


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


eclipsed4utoo:

When running your code I get the following output (given the character 'A'):

A's ASCII integer value is 65.
's ASCII integer value is 13.


Not what I expected. Does it have something to do with the Read metod?
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 1 Sep, 2008 - 07:02 PM
Post #8


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 508



Thanked 45 times

Dream Kudos: 25
My Contributions


Try:
csharp
Console.WriteLine("Please enter a character:");
uint charInt = 0;

while ((charInt = (uint)Console.Read()) != 13)
{
System.Console.WriteLine("{0}'s ASCII integer value is {1}.", (char)charInt, charInt);
}
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:47AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month