Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.15/5 (5 votes)
See more:
can any one give an example for converting string to integer in C# without library functions
Posted
Updated 29-Apr-21 6:11am

In this solution, we are using the ASCI character approach to convert the string into an integer. Basically, ASCII numbers for 0 to 9 are between 48-57. Will use the explicit conversion of a char into integer.

The tricky part here is after each number conversion we should concatenate/add next to the number to it's position. for that we are using num*10.

This helps to add the next number to it's 10th position.

Ex: "123" => num= 0*10+(49-48) = 1
=> num= 1*10+(50-48) = 12
=> num= 12*10+(51-48) = 120+ 3 = 123

C#
public int convertStrToInt(string input){
   int num=0;
   for(int i=0;i<input.Length;i++){
      num = num*10 + ((int)input[i]-48);
   }
   return num
}
 
Share this answer
 
Comments
Richard Deeming 30-Apr-21 12:36pm    
This is basically the same code as solution 2, which was posted 11 years ago.
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.
 
Share this answer
 
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.
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.
 
Share this answer
 
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.
 
Share this answer
 
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;
}
 
Share this answer
 
v4

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