473,418 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Converting a single ASCII character to an int

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?
Aug 30 '06 #1
6 28077
"davetelling" <da*********@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
>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];
Aug 30 '06 #2
*"davetelling" <da*********@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
*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
Aug 30 '06 #3
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

"davetelling" <da*********@discussions.microsoft.comschreef in bericht
news:9C**********************************@microsof t.com...
>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?

Aug 31 '06 #4
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
--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 31 '06 #5
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!

"Cor Ligthert [MVP]" wrote:
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

"davetelling" <da*********@discussions.microsoft.comschreef in bericht
news:9C**********************************@microsof t.com...
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?


Aug 31 '06 #6
davetelling <da*********@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Peter Wilkinson | last post by:
Hello tlistmembers, I am using the encoding function to convert unicode to ascii. At one point this code was working just fine, however, now it has broken. I am reading a text file that has is...
12
by: David Williams | last post by:
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!)....
2
by: Mike Jeffers | last post by:
Hi everyone, I need to convert data from a structure into hexadecimal ascii format. The structure is like this: struct ROOM_DATA { short room_number; short floor_number; long total_area;
8
by: Dan | last post by:
In C#, how would you loop through each character in a string and convert them to their ascii values?
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
12
by: nikNjegovan | last post by:
So i have a tachometer that I can communicated with via UART which gives me a character array of ascii values in the following form: Standard ascii 7 characters including decimal point such that...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
0
by: hillman | last post by:
Hello, Maestros. Following on from the posts on creating structs from a byte. The methods described there work well. But I seem to have struck a snag that some might find interesting. The C/C++...
9
by: Michael Goerz | last post by:
Hi, I am writing unicode stings into a special text file that requires to have non-ascii characters as as octal-escaped UTF-8 codes. For example, the letter "Í" (latin capital I with acute,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.