473,386 Members | 1,693 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,386 software developers and data experts.

Converting characters in string to ascii

Dan
In C#, how would you loop through each character in a string and convert them
to their ascii values?
Nov 17 '05 #1
8 48193
do you mean something like this:

static void Main(string[] args)
{
string test = "ollie";
foreach(char c in test)
{
Console.WriteLine((int)c);
}
Console.ReadLine();
}

HTH

Ollie Riches

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:FB**********************************@microsof t.com...
In C#, how would you loop through each character in a string and convert
them
to their ascii values?

Nov 17 '05 #2
Hi Dan,

Use the Encoding class.

byte[] asciicharacters = Encoding.ASCII.GetBytes(unicodeString);
On Fri, 23 Sep 2005 14:19:03 +0200, Dan <Da*@discussions.microsoft.com> wrote:
In C#, how would you loop through each character in a string and convert them
to their ascii values?


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3
Dan,
string s = "qwerty";

foreach (char c in s)
{
}

All the best,
Phil.

"Dan" wrote:
In C#, how would you loop through each character in a string and convert them
to their ascii values?

Nov 17 '05 #4
Dan
This doesn't answer how to convert the character to its numeric ascii value.
While I'm at it, I'd also like to then convert the resulting number to its
hex equivalent.

"Phil Williams" wrote:
Dan,
string s = "qwerty";

foreach (char c in s)
{
}

All the best,
Phil.

"Dan" wrote:
In C#, how would you loop through each character in a string and convert them
to their ascii values?

Nov 17 '05 #5
you can cast the char to int.
if the value is in the range of 0 to 127 it's the ascii value of the char;
if not it's not a ascii-character.

"Dan" <Da*@discussions.microsoft.com> schrieb im Newsbeitrag
news:DD**********************************@microsof t.com...
This doesn't answer how to convert the character to its numeric ascii
value.
While I'm at it, I'd also like to then convert the resulting number to its
hex equivalent.

"Phil Williams" wrote:
Dan,
string s = "qwerty";

foreach (char c in s)
{
}

All the best,
Phil.

"Dan" wrote:
> In C#, how would you loop through each character in a string and
> convert them
> to their ascii values?

Nov 17 '05 #6
>> This doesn't answer how to convert the character to its numeric ascii
value.
Doesn't System.Encoding get involved here? The string might not be ASCII;
it might be Unicode.
While I'm at it, I'd also like to then convert the resulting number to
its
hex equivalent.


Hex and decimal are not different kinds of numbers. They are different ways
of printing a number. Format strings (String.Format) with the appropriate
specifier can convert a number into the characters that represent it in
either decimal or hex.

Nov 17 '05 #7
Morten Wennevik <Mo************@hotmail.com> wrote:
Use the Encoding class.

byte[] asciicharacters = Encoding.ASCII.GetBytes(unicodeString);


There's no need to in this case - all ASCII values are the same as in
Unicode, although of course there's much more of Unicode.

Just casting the character to an int will give you the Unicode value -
if it's in the ASCII range, the value will be the ASCII value.

--
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
Nov 17 '05 #8
Michael A. Covington <lo**@ai.uga.edu.for.address> wrote:
This doesn't answer how to convert the character to its numeric ascii
value.

Doesn't System.Encoding get involved here?


No - or at least, it doesn't need to.
The string might not be ASCII; it might be Unicode.


*All* strings are Unicode.

All you've got to know is whether a number is above 127 or not, and
there's no need to get Encoding involved to do that :)

--
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
Nov 17 '05 #9

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

Similar topics

1
by: Erinys | last post by:
Hi, I need to access the individual bytes in a string in my javascript function. If the characters in the string were all ascii characters then there would not be a problem, however in my case...
3
by: pabelard | last post by:
I am reading from a file and trying to find out if it has characters above ASCII 127 in it. My sample file does have several of these characters. However, the streamreader seems to skip over...
6
by: davetelling | last post by:
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...
1
by: Kevin S Gallagher | last post by:
I found this code (pretty sure it was from a MVP) for converting a string variable to a form object which works fine within a form. Take the code and place it into a code module and it fails on the...
1
by: ronrsr | last post by:
I have an MySQL database called zingers. The structure is: zid - integer, key, autoincrement keyword - varchar citation - text quotation - text I am having trouble storing text, as typed in...
4
by: Knuckles53 | last post by:
I need some help, my C++ coding skills are a little old and rusty. I need to read in a series of 8-bits stored in a text file. How do I read in these values and get the proper result? For...
0
by: rrp83 | last post by:
Hi All Can anyone fwd me the code for converting a string to bytes similar to the GetBytes function in C# and viceversa?? Regards, RRP83
3
by: =?Utf-8?B?cmtwYXQ=?= | last post by:
i'm calling a C++ DLL from C#. here's C++ interface: char* __cdecl SendCmd(void* handle, char* cmd, char* data, char* buffer); here's my call in C#: unsafe public static extern byte* SendCmd...
4
hsriat
by: hsriat | last post by:
If I need to encode characters below ASCII value 32, I can use this:$text = filter_var($text, FILTER_UNSAFE_RAW, FILTER_FLAG_ENCODE_LOW); How can I encode characters above ASCII value 256?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.