Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Int without int()

I'm trying to implement add2strings, sub2strings, mult2strings functions in Python. They're all very easy if you just do int(string), but I want to do them without that and without importing another cheating thing like Decimal. My current idea is to use bytes.

Is there another way to do this?

like image 410
Eugene K Avatar asked Jul 04 '14 02:07

Eugene K


People also ask

Can you convert a string to an int?

One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.

How do you convert string to int in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .

How do you convert int to string without using library functions in Python?

Convert int to string python without str() function Simple example code keeps dividing a given int value by 10 and prepending the remainder to the output string. Use the ordinal number of '0' plus the remainder to obtain the ordinal number of the remainder, and then convert it to string using the chr function.

Can you convert to int in Python?

In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.

How to convert string to INT in C++?

One effective way to convert a string object into a numeral int is to use the stoi () function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.

Is it possible to convert a string to an integer?

It is important that you do so in a way that does not throw potential exceptions. You can use a couple built in methods, as shown below, to convert a string to int. Both of these would throw an exception if the string value is not a valid integer.

What is the difference between str and int in Python?

The string representation of an integer is more flexible because a string holds arbitrary text data: Each of these strings represent the same integer. Now that you have some foundational knowledge about how to represent integers using str and int, you’ll learn how to convert a Python string to an int.

How do you pass a string to an int in Python?

When you pass a string to int (), you can specify the number system that you’re using to represent the integer. The way to specify the number system is to use base: Now, int () understands you are passing a hexadecimal string and expecting a decimal integer. Technical Detail: The argument that you pass to base is not limited to 2, 8, 10, and 16:


Video Answer


1 Answers

Refer to a basic atoi in C:

int myAtoi(char *str)
{
    int res = 0; // Initialize result

    // Iterate through all characters of input string and update result
    for (int i = 0; str[i] != '\0'; ++i)
        res = res*10 + str[i] - '0';

    // return result.
    return res;
}

Which translates into the Python:

def atoi(s):
    rtr=0
    for c in s:
        rtr=rtr*10 + ord(c) - ord('0')

    return rtr

Test it:

>>> atoi('123456789')
123456789   

If you want to accommodate an optional sign and whitespace the way that int does:

def atoi(s):
    rtr, sign=0, 1
    s=s.strip()
    if s[0] in '+-':
        sc, s=s[0], s[1:]
        if sc=='-':
            sign=-1

    for c in s:
        rtr=rtr*10 + ord(c) - ord('0')

    return sign*rtr

Now add exceptions and you are there!

like image 153
dawg Avatar answered Nov 15 '22 21:11

dawg