Solved

how to convert a numerical string to an integer value without using any in-built conversion functions in c# or java?

Posted on 2010-08-18
3
315 Views
Last Modified: 2013-12-14
I have a numerical string variable as:
string in="4266";

Now, I want to convert this to an integer value as 4266, without using any in-built conversion functions such as: int.Parse().
I just want it to be solved by using the mathematical operators such as Division, Remainder..

Thanks.
0
Comment
Question by:malakheti
3 Comments
 
LVL 4

Expert Comment

by:Scanman999
ID: 33469101
Euh... how can you make a division/remainder operation on a string ?
You need to convert it to a numerical value first... which is exactly your question .... have I missed something ?

I haven't done any C# for many years but isn't there a CINT() or VAL() kind of function to do that ?

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33469796
Does this qualify as a "solution"?

*Assuming only positive, whole integer values:
        private void button1_Click(object sender, EventArgs e)
        {
            string input="4266";
            int output = StringToInt(input);
            MessageBox.Show(input + " <== ? ==> " + output.ToString());
        }

        private int StringToInt(string input)
        {
            string digit;
            int curValue;
            int value = 0;
            for (int i = 0; i < input.Length; i++)
            {
                digit = input.Substring(input.Length - (i + 1), 1);
                curValue = "0123456789".IndexOf(digit);
                value = value + (curValue * (int)Math.Pow(10, i));
            }
            return value;
        }

Open in new window

0
 

Author Closing Comment

by:malakheti
ID: 33470284
Thanks.
This is what I wanted.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question