473,626 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert char[] to int?

Hello

How to convert char[] to int?

Thanks
Nov 17 '05 #1
7 66648
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.Enc oding.ASCII.Get String(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"MilanB" <Mi****@discuss ions.microsoft. com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.Enc oding.ASCII.Get String 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.Enc oding.ASCII.Get String(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"MilanB" <Mi****@discuss ions.microsoft. com> wrote in message
news:9A******** *************** ***********@mic rosoft.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****@discuss ions.microsoft. com> wrote:
Hi
Thanks for replay...
When I use Convert.ToInt32 , I get exception "Specified cast is not valid.",
With System.Text.Enc oding.ASCII.Get String 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.Enc oding.ASCII.Get String(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"MilanB" <Mi****@discuss ions.microsoft. com> wrote in message
news:9A******** *************** ***********@mic rosoft.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(Encod ing.ASCII.GetSt ring(Encoding.A SCII.GetBytes(d )));

Hope this helps

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

"MilanB" <Mi****@discuss ions.microsoft. com> wrote in message
news:95******** *************** ***********@mic rosoft.com...
Hi
Thanks for replay...
When I use Convert.ToInt32 , I get exception "Specified cast is not valid.", With System.Text.Enc oding.ASCII.Get String 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.Enc oding.ASCII.Get String(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"MilanB" <Mi****@discuss ions.microsoft. com> wrote in message
news:9A******** *************** ***********@mic rosoft.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.sqejicg 8klbvpo@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****@discuss ions.microsoft. com> wrote:
Hi
Thanks for replay...
When I use Convert.ToInt32 , I get exception "Specified cast is not valid.", With System.Text.Enc oding.ASCII.Get String 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.Enc oding.ASCII.Get String(char[]).

Hope this helps

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"MilanB" <Mi****@discuss ions.microsoft. com> wrote in message
news:9A******** *************** ***********@mic rosoft.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
24519
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource = source.GetBuffer(0 ); but i need a char* and so this is not working CString source = _T("TestString");
1
2513
by: Sam Smith | last post by:
Hi, I wan't a function to take a const char*, a start bit position and number of bits and convert that bit-stream into a primitive of desired type. I.e. something like: char convert(const unsigned char* buffer, size_t start_pos, size_t length) { char value = 0;
5
22026
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate example). int foo(const char* argv) { return 0; } int main(int argc, char* argv) {
7
7094
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done without strtol or s/printf function. Thanks, whatluo.
15
10820
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
19
11300
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
3
2249
by: Eric BOUXIROT | last post by:
hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_CalculAXplusB(short int *IN_Tab_entree ,int IN_taille , double *OUT_Tab_sortie_freq , double *OUT_Tab_sortie, double IN_A,
12
10077
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
5
38521
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char to string. I have a function that accepts a string, and I wish to access this function for each character in the string, individually. The only solution I have found is: char c = 'A'; string s = System.Text.Encoding.ASCII.GetString(c);
0
8259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8192
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7188
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.