How to convert char[] to int?

  • Thread starter Thread starter Guest
  • Start date Start date
Hi,

If I understand correctly you have a char[] of numeric digits and you want
to convert this to the integer number represented by the digits.

You could convert the char[] to a string then using int.Parse or
Contert.ToInt32.
To convert the char[] use System.Text.Encoding.ASCII.GetString(char[]).

Hope this helps
 
Hi
Thanks for replay...
When I use Convert.ToInt32, I get exception "Specified cast is not valid.",
With System.Text.Encoding.ASCII.GetString I get compiler error: " Argument
'1': cannot convert from 'char[]' to 'byte[]'"
int.Parse has not overload method to pass char[]

Sorry
Thanks



Chris Taylor said:
Hi,

If I understand correctly you have a char[] of numeric digits and you want
to convert this to the integer number represented by the digits.

You could convert the char[] to a string then using int.Parse or
Contert.ToInt32.
To convert the char[] use System.Text.Encoding.ASCII.GetString(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
MilanB said:
Hello

How to convert char[] to int?

Thanks
 
Hi,

You must first convert the chars to bytes, here is a quick and dirty.

char[] d = {'1', '2'};
int i = int.Parse(Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(d)));

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor

MilanB said:
Hi
Thanks for replay...
When I use Convert.ToInt32, I get exception "Specified cast is not valid.",
With System.Text.Encoding.ASCII.GetString I get compiler error: " Argument
'1': cannot convert from 'char[]' to 'byte[]'"
int.Parse has not overload method to pass char[]

Sorry
Thanks



Chris Taylor said:
Hi,

If I understand correctly you have a char[] of numeric digits and you want
to convert this to the integer number represented by the digits.

You could convert the char[] to a string then using int.Parse or
Contert.ToInt32.
To convert the char[] use System.Text.Encoding.ASCII.GetString(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
MilanB said:
Hello

How to convert char[] to int?

Thanks
 
Hi MilanB,

The method to convert a char[] to string is like this.

string s = new string(char[]);
int i = Int32.Parse(s);

Hi
Thanks for replay...
When I use Convert.ToInt32, I get exception "Specified cast is not valid.",
With System.Text.Encoding.ASCII.GetString I get compiler error: " Argument
'1': cannot convert from 'char[]' to 'byte[]'"
int.Parse has not overload method to pass char[]

Sorry
Thanks



Chris Taylor said:
Hi,

If I understand correctly you have a char[] of numeric digits and you want
to convert this to the integer number represented by the digits.

You could convert the char[] to a string then using int.Parse or
Contert.ToInt32.
To convert the char[] use System.Text.Encoding.ASCII.GetString(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
MilanB said:
Hello

How to convert char[] to int?

Thanks
 
Yes, that is even better! Much better.

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
Morten Wennevik said:
Hi MilanB,

The method to convert a char[] to string is like this.

string s = new string(char[]);
int i = Int32.Parse(s);

Hi
Thanks for replay...
When I use Convert.ToInt32, I get exception "Specified cast is not valid.",
With System.Text.Encoding.ASCII.GetString I get compiler error: " Argument
'1': cannot convert from 'char[]' to 'byte[]'"
int.Parse has not overload method to pass char[]

Sorry
Thanks



Chris Taylor said:
Hi,

If I understand correctly you have a char[] of numeric digits and you want
to convert this to the integer number represented by the digits.

You could convert the char[] to a string then using int.Parse or
Contert.ToInt32.
To convert the char[] use System.Text.Encoding.ASCII.GetString(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
Hello

How to convert char[] to int?

Thanks
 
Back
Top