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
303 Views
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
Question by:malakheti

LVL 4

Expert Comment

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

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;

}
``````
0

Author Closing Comment

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

## Featured Post

### Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…