473,729 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28105
"davetellin g" <da*********@di scussions.micro soft.comwrote in message
news:9C******** *************** ***********@mic rosoft.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
*"davetellin g" <da*********@di scussions.micro soft.comwrote in message
news:9C******** *************** ***********@mic rosoft.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.UInt3 2(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

"davetellin g" <da*********@di scussions.micro soft.comschreef in bericht
news:9C******** *************** ***********@mic rosoft.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

"davetellin g" <da*********@di scussions.micro soft.comschreef in bericht
news:9C******** *************** ***********@mic rosoft.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*********@di scussions.micro soft.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.co m>
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
10700
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 in unicode (I am unsure of which flavour or bit depth). as I read in the file one line at a time (readlines()) it converts to ascii. Simple enough. At the same time I am copressing to bz2 with the bz2 module but that works just fine. The code...
12
70543
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!). Could anyone help me out? Thanks Dave
2
4037
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
48274
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
3544
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 entered. I know what the Ascii value of each letter, so i understood that i can transfer my string to BYTE and enter the ascii value by myself. if someone has a better idea, i'll be happy to hear about it. how can i know the Unicode value of a...
12
8540
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 the array when printed would equal the rpm value ie 2031.00 rpm = I've been rummaging everywhere and can't find anything to tackle this. I would be extreemly greatful to anyone that can point me in the right
2
4378
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. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the fields I decode tells me which the latter fields are). The code I have converts a single byte of...
0
1030
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++ structs that give problems all have the same issue: they all have at least one field something like this: char character There does not appear to be any way to do this in c#. The closest I could get was fixed sbyte character. This does indeed...
9
11570
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, code point 205) would come out as "\303\215". I will also have to read back from the file later on and convert the escaped characters back into a unicode string.
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.