Make your own atoi() function in C

Spread the love

atoi() is able to translate a string of a number into an integer number. There are lots of tech to do, and here is showing some examples.

[code lang=”c”]
int my_atoi(char *p) {
int k = 0;
while (*p) {
k = k*10 + (*p) – ‘0’;
p++;
}
return k;
}
[/code]

10 times can be switched to bit-wise shift since bit-wise will use smaller instruction set which enables to reduce transistor in a system. Following shows of it.

[code lang=”c”]
int my_atoi(char *p) {
int k = 0;
while (*p) {
k = (k<<3)+(k<<1)+(*p)-‘0’;
p++;
}
return k;
}
[/code]

17 Replies to “Make your own atoi() function in C”

    1. k holds an integer, and it is recalculated by looking at the each character from the beginning given p. (*p) tells you the character’s ASCII code and ‘0’ as well. The difference gives the actual integer number. k*10 is shifting of previous value.

      For example, if p is given by “425”,

      The first calculation of k = 0*10 + (*p) – ‘0’ = 0 + 52-48 = 4
      The next calculation of k = 4*10 + (*p) – ‘0’ = 40 + 50 – 48 = 42
      The final calculation of k = 42*10 + (*p) – ‘0’ = 420 + 53-48 = 425

      Character ASCII code of “4” is 52, “2” is 50, “5” is 53, and “0” is 48.

      Hope this would help you,
      Daniel

  1. Hi Daniel,

    I have this doubt about the code you posted, you said in one of the comments that k has an ASCII equivalent. Does the atoi version you wrote changes its behaviour if we use other encoding rather than ASCII?, say UTF-8.

    Great blog.


    Carlos

    1. Thanks, Carlos.

      You’re right. It doesn’t support UTF-8, and the code I made was only for simple implementation showing how atoi() can be implemented, and it would be useful for job interview or some sort of things. It’s not fully for a product readiness or something like that.

      Thank you for your interest and pointing out of it.

      Daniel

  2. Nice. For those of you who can’t figure why k << 3 + k << 1 is used instead.

    k * 10 = k * 8 + k * 2 = k * 2^3 + k * 2 ^ 1 = k << 3 + k << 1

    So k * 10 + *p – '0' is nothing but k<<3 + k<<1 + *p – '0'.

    1. No. The simple function doesn’t check the general error cases.

      As I’ve mentioned, this atoi() is just showing the simple way to do it, and want to show the “shift” is a little better way in performance in Small Embedded System.

  3. in the first post inside while loop add following conditions. then it works like atoi for negative and invalid inputs

    if(*p == ‘-‘ && i == 0)
    {
    isNegative = TRUE;
    p++;
    continue;
    }

    if(*p ‘9’)
    break;

    1. No, you are confusing the associative property of addition and associative property of multiplication.

      k*2^3+k*2=k*(2^3+2). It cannot be k*(2^4) because the power cannot be combined with addition. If it is multiplication, then you can combine it.

      So, the code of my logic is correct,

      Thanks,
      Daniel

  4. [code lang=”c”]
    #include<stdio.h>
    #include<stdint.h>

    #define Is_NUMERIC_STRING(d) (*(char*)d >= 48) && (*(char*)d<= 57)

    uint32_t
    StringToInt(const char *pszBuffer) {

    uint32_t u32Number=0;

    while( Is_NUMERIC_STRING(pszBuffer)) {

    u32Number=(u32Number*10)+ (*pszBuffer-48);
    pszBuffer++;
    }

    return u32Number;
    }

    int main() {
    uint32_t d;

    d=StringToInt("1230");

    printf("%u\n",d);

    return 0;
    }
    [/code]

    For more detail you can see this link: http://www.aticleworld.com/2016/03/how-to-use-atoi-and-how-to-make-own-atoi.html

Leave a Reply

Your email address will not be published. Required fields are marked *