PC Review


Reply
Thread Tools Rate Thread

Converting characters in string to ascii

 
 
=?Utf-8?B?RGFu?=
Guest
Posts: n/a
 
      23rd Sep 2005
In C#, how would you loop through each character in a string and convert them
to their ascii values?
 
Reply With Quote
 
 
 
 
Ollie Riches
Guest
Posts: n/a
 
      23rd Sep 2005
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" <(E-Mail Removed)> wrote in message
news:FB27F402-9BFC-4267-8FE0-(E-Mail Removed)...
> In C#, how would you loop through each character in a string and convert
> them
> to their ascii values?



 
Reply With Quote
 
Morten Wennevik
Guest
Posts: n/a
 
      23rd Sep 2005
Hi Dan,

Use the Encoding class.

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


On Fri, 23 Sep 2005 14:19:03 +0200, Dan <(E-Mail Removed)> 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]
 
Reply With Quote
 
=?Utf-8?B?UGhpbCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      23rd Sep 2005
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?

 
Reply With Quote
 
=?Utf-8?B?RGFu?=
Guest
Posts: n/a
 
      23rd Sep 2005
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?

 
Reply With Quote
 
Christof Nordiek
Guest
Posts: n/a
 
      23rd Sep 2005
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" <(E-Mail Removed)> schrieb im Newsbeitrag
newsD173067-0AEB-46C2-8F00-(E-Mail Removed)...
> 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?



 
Reply With Quote
 
Michael A. Covington
Guest
Posts: n/a
 
      23rd Sep 2005
>> 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.



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Sep 2005
Morten Wennevik <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Sep 2005
Michael A. Covington <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Byte Converting for *8*-bit ASCII characters clintp Microsoft C# .NET 5 11th Mar 2005 07:22 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jay B. Harlow [MVP - Outlook] Microsoft C# .NET 7 1st Aug 2003 06:03 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Mikael Jansson Microsoft C# .NET 0 31st Jul 2003 08:42 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Jon Skeet Microsoft C# .NET 0 31st Jul 2003 08:38 PM
Re: Converting a string to a string that contains the ASCII values of each letter in the origional string Frank Oquendo Microsoft C# .NET 0 31st Jul 2003 08:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 AM.