Richard Hough

Is there a simple way to convert a char digit to its integer value Currently I'm using:

if (Char.IsDigit(codeChar))
codeInt = Convert.ToInt32(codeChar) - Convert.ToInt32('0');


Re: .NET Base Class Library int value of char digits?

OmegaMan

Try this

int ch = System.Text.Encoding.ASCII.GetBytes("c")[0];





Re: .NET Base Class Library int value of char digits?

Peter Ritchie

I would suggest Int32.Parse, but it doesn't accept a character, you'd have to use a string rather than a char.




Re: .NET Base Class Library int value of char digits?

decyclone

Hi Richard,

Try this :

Char ch = '.',basenumber = '0';
Int32 i = 0;
if (Char.IsNumber(ch))
{
i = (Int32)(ch - basenumber);
}
MessageBox.Show(i.ToString());





Re: .NET Base Class Library int value of char digits?

andypai

Hi Richard,

Maybe something like this will work...

Code Snippet

char a = '7';

if (Char.IsNumber(a))

{

int b = Int32.Parse(a.ToString());

Response.Write(b.ToString());

}

Hope that helps






Re: .NET Base Class Library int value of char digits?

Matthew Watson

I think decyclone's answer is the simplest.

Because you can just do:

char c = '3';
int n = c - '0';