Page 1 of 1

how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:07 am
by Sherry123
how to make 00008 to become 8? in C language for Raspberry pi

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:08 am
by scotty101
Is 00008 currently a string?

Do you have an example of how this value is currently defined?

Something like below would work

Code: Select all

#include <stdio.h>

void main(void)
{
    char myString[] = "00008";
    int number;

    number = atoi(myString);
    printf("Hello World! %i\n", number);
}

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:10 am
by jamesh
Without a bit more information, not possible to answer.

Presumably, 000008 is a string, but do you want to convert to a single character string "8", a single character '8', or an actual number e.g. `int num = 8`, or perhaps `double num = 8`?

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:13 am
by n67
Well, as a starting point, the obvious answer is:

atoi()

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:45 am
by PeterO
Just for completeness ... :roll:

No one has noted that 00008 is in fact not a valid number in C.

PeterO

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:47 am
by DougieLawson
PeterO wrote:
Mon Mar 12, 2018 11:45 am
Just for completeness ... :roll:

No one has noted that 00008 is in fact not a valid number in C.

PeterO
I did. It has a leading zero so is octal and an eight isn't a valid octal digit.

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:48 am
by jahboater
Did you know that 0 is an octal literal?
:)

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 11:55 am
by PeterO
n67 wrote:
Mon Mar 12, 2018 11:13 am
Well, as a starting point, the obvious answer is:

atoi()
atoi() is only the right answer if the string is guaranteed to hold a valid non-zero number.
In general strtol() is better choice as failure to convert the string to a number can be detected.

PeterO

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 12:28 pm
by jahboater
PeterO wrote:
Mon Mar 12, 2018 11:55 am
atoi() is only the right answer if the string is guaranteed to hold a valid non-zero number.
In general strtol() is better choice as failure to convert the string to a number can be detected.
and you can explicitly force the base to be 10.

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 1:11 pm
by PeterO
jahboater wrote:
Mon Mar 12, 2018 12:28 pm
and you can explicitly force the base to be 10.
More interestingly you can set it to bases other than 10 8-)

In the OP's case it does look like explicitly setting the base to 10 (and not relying on the default 0) is needed otherwise the string "0000008" will fail when it tries to convert from octal due to the leading zero.
PeterO

Re: how to make 00008 to become 8? in C language for Raspberry pi

Posted: Mon Mar 12, 2018 1:21 pm
by jahboater
Exactly.

which to be fair is what atoi() does (from the man page):

Code: Select all

       The behavior is the same as

           strtol(nptr, NULL, 10);

       except that atoi() does not detect errors.
but like you, I am uncomfortable with the last line!