Click here to Skip to main content
14,739,811 members
Please Sign up or sign in to vote.
4.00/5 (4 votes)
See more:
can any one give an example for converting string to integer in C# without library functions
Posted

A somewhat less convoluted example:

int StrToInt(string str)
{
    int response = 0;
    foreach (char c in str)
    {
        response *= 10;
        response += c - '0';
    }
    return response;
}


Obviously some extra code needs to be added for error checking, sign etc, but I'm sure you can figure that out quite easily.
   
But why? Since the framework is already loaded, why not make use of the functionality instead of reinventing the wheel? This question makes no sense at all.
   
Comments
tausif3 8-Apr-14 3:04am
   
These are generally academic question.
Generally practiced to fetch the internal working details of framework.
#realJSOP 9-Apr-14 6:31am
   
You replied to a four-year old comment. I no longer really care.
Member 13769056 9-Apr-18 0:14am
   
Four years later, nobody cares that you no longer really care.
One way to do it is through code. Not pretty, but here it is.
This can only be used for string inputs that are basically integers (e.g.123,43785 etc) and works for only positive integers - but seeing this you'll get the general idea :) .

public static void Main() {
    string str = Console.ReadLine();
    int j = 0;
    int myNumber = 0;
    string strReverse =String.Empty;

    //Reverse the string
    foreach (char temp in str)
    {
        strReverse = temp + strReverse;
    }

    foreach (char temp in strReverse)
    {
        int i = temp - 48; //Ascii character
        myNumber = myNumber + i * myPower(10,j);
        j++;
    }
    Console.WriteLine(myNumber);
    Console.ReadLine();
}


public static int myPower(int i, int j)
{
    int final = 1;
    for (int loop =0 ; loop < j; loop++)
        final = final  * i;
    return final;
}
   
v4
Good question and I'll explain the solution using a specific number as an example

e.g. input = "3894"

The numeric value of one character in the string can be obtained via a lookup table. Here I'll use a string as the lookup table.

LookupTable = "0123456789"

The important property of this lookup table is that the numeric value of a character is it's index in the table.

e.g. the value of the character '3' is 3 because it is found at LookupTable[3]


The total value of the input string could be obtained via the calculation

3894 = 4 + (9 * 10) + (8 * 100) + (3 * 1000)

but this can be rearranged so that it can be coded into a simple loop.

3894 = ((((((3 * 10) + 8) * 10) + 9) * 10) + 4)


public Int32 DecimalStrToInt(String input) {
  const String LookupTable = "0123456789";
  Int32 charValue;
  Char c;
  Int32 total = 0;
  Int32 index = 0;
  while (index < input.Length) {
    c = input[index];
    charValue = LookupTable.IndexOf(c);
    total = (total * 10) + charValue;
    index++;
  }
  return total;
}


It's a trivial task to modify the LookupTable and multiplier to handle any numeric base, e.g. base 16.

public Int32 HexStrToInt(String input) {
  const String LookupTable = "0123456789ABCDEF";
  Int32 charValue;
  Char c;
  Int32 total = 0;
  Int32 index = 0;
  while (index < input.Length) {
    c = input[index];
    charValue = LookupTable.IndexOf(c);
    total = (total * 16) + charValue;
    index++;
  }
  return total;
}


A real implementation would need to handle invalid inputs and result overflow.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900