PC Review


Reply
Thread Tools Rating: Thread Rating: 3 votes, 1.67 average.

how to convert string to ascii value in C#

 
 
=?Utf-8?B?R1A=?=
Guest
Posts: n/a
 
      21st Jun 2004
Hi ,
I want to convert each letter in a string to it's ascii character.
I have tried using System.Convert.ToInt32(C)
where c is the substring character from the main string.
When I use this syntax I get a message string input format incorrect.But when issue the same syntax in the immediate window like System.Convert.ToInt32('A'),it returns the correct ascii value 65.I am not sure if I miss something .
Can some please let me know how to convert a char value to ASCII number.
Thanks
GP
 
Reply With Quote
 
 
 
 
=?Utf-8?B?R1A=?=
Guest
Posts: n/a
 
      21st Jun 2004
I found the answer,for those who are looking for a solution...here is the solution....

//check for atleast one upper case
System.Text.Encoding ascii = System.Text.Encoding.ASCII;
Byte[] encodedBytes = ascii.GetBytes(str);
foreach (Byte b in encodedBytes)
{
if ((b >= 97) && (b <= 122))
bUppercase=true;
}


"GP" wrote:

> Hi ,
> I want to convert each letter in a string to it's ascii character.
> I have tried using System.Convert.ToInt32(C)
> where c is the substring character from the main string.
> When I use this syntax I get a message string input format incorrect.But when issue the same syntax in the immediate window like System.Convert.ToInt32('A'),it returns the correct ascii value 65.I am not sure if I miss something .
> Can some please let me know how to convert a char value to ASCII number.
> Thanks
> GP

 
Reply With Quote
 
Rico Wind
Guest
Posts: n/a
 
      21st Jun 2004

"GP" wrote in message
> Hi ,
> I want to convert each letter in a string to it's ascii character.
> I have tried using System.Convert.ToInt32(C)
> where c is the substring character from the main string.
> When I use this syntax I get a message string input format incorrect.But

when issue the same syntax in the immediate window like
System.Convert.ToInt32('A'),it returns the correct ascii value 65.I am not
sure if I miss something .
> Can some please let me know how to convert a char value to ASCII number.

Try this
string s = "hallo world";
foreach( char c in s)
{
Console.WriteLine(System.Convert.ToInt32(c));
}
Console.ReadLine();

/Rico Wind
> Thanks
> GP



 
Reply With Quote
 
Niki Estner
Guest
Posts: n/a
 
      21st Jun 2004
"GP" <(E-Mail Removed)> wrote in
news:A5379D85-39FC-49B6-9052-(E-Mail Removed)...
> Hi ,
> I want to convert each letter in a string to it's ascii character.


First of all, .net strings are stored as unicode, so you won't get ASCII
values that way, you'll get unicode values. If you really need ASCII values,
you should have a look at ASCIIEncoding or StreamWriter class.

> I have tried using System.Convert.ToInt32(C)
> where c is the substring character from the main string.


Is it a substring or a character?
This will work fine:
System.Convert.ToInt32("ABC"[0])
Because the [] operator returns a character, however in:
Console.WriteLine(System.Convert.ToInt32("ABC".Substring(0,1)));
Substring returns a string, and ToInt32 tries to parse that string - which
fails, because "A" is not a number.

> When I use this syntax I get a message string input format incorrect.But

when issue the same syntax in the immediate window like
System.Convert.ToInt32('A'),it returns the correct ascii value 65.I am not
sure if I miss something .

System.Convert.ToInt32("A") would be the same syntax. Use [] (or () in VB, I
think) instead of Substring.

> Can some please let me know how to convert a char value to ASCII number.


That's not your problem. The problem is getting the char value. To make
things easier, use a temporary variable like:
char c = MyString[index];
int unicodeValue = System.Convert.ToInt32(c);

- the compiler will give you an error if your code doesn't do what you think
(i.e. doesn't return a char)
- you can look at the temp variables in the debugger
- this way will work.

Niki


 
Reply With Quote
 
Niki Estner
Guest
Posts: n/a
 
      21st Jun 2004
NOOO!!!

That's not the solution, that's a dirty hack!
Many languages (like German, French, Russian, Chinese, Japanese...) have
uppercase letters outside the 'A'-'Z' range, like ÄÖÜÁÀÙ.
Use:
foreach (char c in str) bUppercase |= char.IsUpper(c);
Shorter, better, portable.

Niki

(Writing from Germany)


"GP" <(E-Mail Removed)> wrote in
news:41A1CEFF-877B-4238-B743-(E-Mail Removed)...
> I found the answer,for those who are looking for a solution...here is the

solution....
>
> //check for atleast one upper case
> System.Text.Encoding ascii = System.Text.Encoding.ASCII;
> Byte[] encodedBytes = ascii.GetBytes(str);
> foreach (Byte b in encodedBytes)
> {
> if ((b >= 97) && (b <= 122))
> bUppercase=true;
> }
>
>
> "GP" wrote:
>
> > Hi ,
> > I want to convert each letter in a string to it's ascii character.
> > I have tried using System.Convert.ToInt32(C)
> > where c is the substring character from the main string.
> > When I use this syntax I get a message string input format incorrect.But

when issue the same syntax in the immediate window like
System.Convert.ToInt32('A'),it returns the correct ascii value 65.I am not
sure if I miss something .
> > Can some please let me know how to convert a char value to ASCII number.
> > Thanks
> > GP



 
Reply With Quote
 
Jimi
Guest
Posts: n/a
 
      21st Jun 2004
I think it's:

(int)cSomeChar


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jun 2004
Niki Estner <(E-Mail Removed)> wrote:
> "GP" <(E-Mail Removed)> wrote in
> news:A5379D85-39FC-49B6-9052-(E-Mail Removed)...
> > Hi ,
> > I want to convert each letter in a string to it's ascii character.

>
> First of all, .net strings are stored as unicode, so you won't get ASCII
> values that way, you'll get unicode values. If you really need ASCII values,
> you should have a look at ASCIIEncoding or StreamWriter class.


Both of those are going to be slower than just using a cast to int to
get the Unicode value, and then detecting whether or not it's in the
ASCII range. We're fortunate enough to know that all ASCII values have
the same values in Unicode.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~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
Convert Ascii string to decimal friend Microsoft VB .NET 8 30th Apr 2009 12:34 PM
Convert a Widestring to an Ascii String? Marc Microsoft C# .NET 5 19th Nov 2007 09:21 AM
[C#] Convert a string in ascii to unicode =?Utf-8?B?RnJlZGR5Ym95?= Microsoft Dot NET Compact Framework 11 23rd Dec 2005 12:40 PM
Convert ascii code to string Daniel Passwater via DotNetMonster.com Microsoft Dot NET Framework Forms 2 28th Nov 2004 02:37 AM
Convert a string to Ascii codes and then back to string again Kai Bohli Microsoft C# .NET 11 8th Jul 2004 03:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:15 AM.