467,894 Members | 1,748 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

How to convert char[] to int?

Hello

How to convert char[] to int?

Thanks
Nov 17 '05 #1
  • viewed: 61221
Share:
7 Replies
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" <Mi****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
Hello

How to convert char[] to int?

Thanks

Nov 17 '05 #2
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" wrote:
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" <Mi****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
Hello

How to convert char[] to int?

Thanks


Nov 17 '05 #3
Hi MilanB,

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

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

On Sat, 07 May 2005 12:20:01 +0200, MilanB <Mi****@discussions.microsoft.com> wrote:
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" wrote:
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" <Mi****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
> Hello
>
> How to convert char[] to int?
>
> Thanks



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4
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" <Mi****@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
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" wrote:
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" <Mi****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
Hello

How to convert char[] to int?

Thanks


Nov 17 '05 #5
Yes, that is even better! Much better.

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.sqejicg8klbvpo@stone...
Hi MilanB,

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

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

On Sat, 07 May 2005 12:20:01 +0200, MilanB <Mi****@discussions.microsoft.com> wrote:
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" wrote:
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" <Mi****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
> Hello
>
> How to convert char[] to int?
>
> Thanks


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #6
Thank you Chris
You helped me.
Nov 17 '05 #7
Thank you Morten
You helped me with very elegant way.

Milan

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Markus Hämmerli | last post: by
1 post views Thread by Sam Smith | last post: by
5 posts views Thread by Brad Moore | last post: by
15 posts views Thread by Yifan | last post: by
19 posts views Thread by caramel | last post: by
3 posts views Thread by Eric BOUXIROT | last post: by
5 posts views Thread by Zytan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.