how to convert string to ascii value in C#


G

Guest

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
 
Ad

Advertisements

G

Guest

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;
}
 
R

Rico Wind

"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
 
N

Niki Estner

GP said:
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
 
N

Niki Estner

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 said:
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 said:
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 .
 
J

Jon Skeet [C# MVP]

Niki Estner said:
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.
 
Ad

Advertisements

Joined
Aug 14, 2013
Messages
1
Reaction score
0
U can try this


String c = "I miss U";
foreach(char n in c)
{
int d = n;
console.writeline(System.convert.ToString(d,2));

}
console.ReadKey();
}




i believe this would help u out... thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top