Converting a string of digits to an integer, storing the result

Go To StackoverFlow.com

0

Sorry for the confusing question, but what I'm trying to do is store an array in a variable.

I want to store the numbers in *value so that instead of int value: -12118433669 it will be int value: 123456789.

OUTPUT

123456789
array: '123456789' int value: -1218433669
0001234
array: '0001234' int value: -1218433669
5
array: 'abc5xyz' int value: -1218433669

array: '' int value: -1218433669
987654321
array: '987654321' int value: -1218433669

SOURCE

#include <stdio.h>

MyFNatoi(char *numArray, int *value) {

    int i;

    for (i = 0; i < 10; i++) {
        if (numArray[i] > 47 && numArray[i] < 58) {
            printf("%c", numArray[i] - 0);
        }
    }

}

int main() {

    char numbers[5][10] = { "123456789", "0001234", "abc5xyz", "", "987654321" };
    int i, value;

    for(i = 0; i < 5; i++) {
        MyFNatoi(numbers[i], &value);
        printf("\narray: '%s' int value: %d\n", numbers[i], value);
    }
    return 0;

}
2012-04-05 00:01
by eveo
You never assign anything to *value, so what do you expect to happen - Oliver Charlesworth 2012-04-05 00:04
Hence the point of this question, how would I store those values in *value? Don't down vote me for no reason - eveo 2012-04-05 00:04
Ok, well in that case: what have you tried?Oliver Charlesworth 2012-04-05 00:05
I tried doing *value = numArray[1] + (numArray[2] * 10) + (numArray[3] * 100) and so forth but that's a long and inefficient method - eveo 2012-04-05 00:07
That's essentially what you have to do (although you need to check your indices). Alternatively, you may find atoi or sscanf from the standard library helpful - Oliver Charlesworth 2012-04-05 00:08
I can only use stdio.h - eveo 2012-04-05 00:10
Why? Anyway, sscanf is in <stdio.h> - Oliver Charlesworth 2012-04-05 00:11
Do you know how to scan in array values into a pointer? sscanf(numArray[i], "%d", &value); isn't working - eveo 2012-04-05 00:37


0

I propose this function:

void MyFNatoi(const char *str, int *value)
{
    if (value == NULL)
        return;

    *value = 0;

    if (str != NULL)
    {
        int negative = 0;

        if (*str == '-')
        {
            negative = 1;
            str++;
        }

        while (*str && isdigit(*str))
        {
            *value = (*value * 10) + (*str++ - '0');
        }

        if (negative)
            *value *= -1;
    }
}

It handles negative numbers, and only checks leading digits (so wont make a number out of "abc123def456" for example).

2012-04-05 05:54
by Some programmer dude
Nothing in the original question suggests the need to handle negatives. Its a nice touch, but over-coding leads to code-bloat and risks bugs - abelenky 2012-04-05 12:57


-1

My Version:

void MyFNatoi(char *numArray, int *value)
{
    for (*value = 0; *numArray != '\0'; ++numArray) {
        if (0x30 <= *numArray && *numArray <= 0x39) {
            *value = 10* *value + *numArray - 0x30;
        }
    }
}
2012-04-05 00:55
by abelenky
Why use the comparison instead of the more convenient isdigit function? Also, instead of subtracting the magic number 0x30 you could subtract '0' to make it more obvious - Some programmer dude 2012-04-05 05:47
The original code used comparison instead of isdigit. Why change the original code? 0x30 is perfectly obvious to me, and makes more sense than a character literal. Just different perspectives for different people - abelenky 2012-04-05 05:53


-1

#define Zero '0'
#define Nine '9'

void MyFNatoi(char *numArray, int *value) {

    int i;
    *value = 0;
    for (i = 0; i < 10 && numArray[i] != 0; i++) {
        if (numArray[i] >= Zero && numArray[i] <= Nine) {
            *value = *value * 10 + (numArray[i] - Zero);
        }
    }

}
2012-04-05 10:14
by BLUEPIXY
Ads