By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

How to convert char[] to int?

P: n/a
Hello

How to convert char[] to int?

Thanks
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Thank you Chris
You helped me.
Nov 17 '05 #7

P: n/a
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.