Converting a single ASCII character to an int

G

Guest

I am a total newbie, trying to slog through the Visual C# Express
application. I need to be able to convert a single ASCII character (can be
anything from 0 to 255) to an int for use in other places. So far, I cannot
find anything that works. My application gets a string of characters from an
external device via the serial port. I can use the substring method to get
just one character from that input string, and I need to be able to convert
that character's ASCII value to an int. Is there a straightforward way to do
this, similar to atoi in C?
 
J

John Vottero

davetelling said:
I am a total newbie, trying to slog through the Visual C# Express
application. I need to be able to convert a single ASCII character (can be
anything from 0 to 255) to an int for use in other places. So far, I
cannot
find anything that works. My application gets a string of characters from
an
external device via the serial port. I can use the substring method to get
just one character from that input string, and I need to be able to
convert
that character's ASCII value to an int. Is there a straightforward way to
do
this, similar to atoi in C?

string theString = "ABC";
int asciiA = theString[0];
int asciiB = theString[1];
int asciiC = theString[2];
 
M

Michael

*I am a total newbie, trying to slog through the Visual C# Express
* application. I need to be able to convert a single ASCII character (can be
* anything from 0 to 255) to an int for use in other places. So far, I
cannot
* find anything that works. My application gets a string of characters from
an
* external device via the serial port. I can use the substring method to get
* just one character from that input string, and I need to be able to
convert
* that character's ASCII value to an int. Is there a straightforward way to
do
* this, similar to atoi in C?
*
davetelling,

If you have a System.String reference, you can use the String class' indexer
to get individual elements.
From here, you may look at using the System.Convert class, specifically its
ToUInt32 static method.

An example of usage:
----------------------

string input = "Hello World!";
uint code;

foreach( char c in input )
{
code = ConvertTo.UInt32(c);
System.Console.WriteLine("{0}'s integer value is {1}." , c , code );
}

-MH
 
C

Cor Ligthert [MVP]

davetelling,

In addition to the given answers, be aware that ASCII is a 7 bit character
set (0-127).

Therefore you will get seldom classified information in converting the non
official extended ASCII character set which were created in all kind of
tastes by Microsoft/IBM in the first days of the PC which was using an 8bit
processor.

Just as addition.

Cor
 
M

Morten Wennevik

It is not necessary to Convert. As John pointed out, you can do this very
easily.
An int is a wider data type than char so it can hold a char with implicit
conversion.

char c = 'ø';
int d = c;

Note also that ASCII is only characters 0-127, characters 128-255 varies
with code tables, and on my computer d == 248
 
G

Guest

I appreciate the various replies. In my application, the data coming in are
not really ASCII - they are just bytes that represent data values coming from
a device with a range of 0-255, so the simple method of int asciiA =
string[x]; seems to work fine. I will try the other methods to see if they
can be used in other areas, however.
Thanks again!
 
J

Jon Skeet [C# MVP]

davetelling said:
I appreciate the various replies. In my application, the data coming in are
not really ASCII - they are just bytes that represent data values coming from
a device with a range of 0-255

In that case you should read them as binary data (bytes) instead of
text data (chars). Don't convert them into text data at all. If you
arbitrarily convert binary data to text data, sooner or later you're
pretty much bound to run into issues.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top