Sherry123
Posts: 18
Joined: Wed Mar 07, 2018 10:40 am

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

Mon Mar 12, 2018 11:07 am

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

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

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

Mon Mar 12, 2018 11:08 am

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);
}
Last edited by scotty101 on Mon Mar 12, 2018 11:12 am, edited 1 time in total.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26659
Joined: Sat Jul 30, 2011 7:41 pm

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

Mon Mar 12, 2018 11:10 am

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`?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

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

Mon Mar 12, 2018 11:13 am

Well, as a starting point, the obvious answer is:

atoi()
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
PeterO
Posts: 5879
Joined: Sun Jul 22, 2012 4:14 pm

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

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

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

Mon Mar 12, 2018 11:47 am

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

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

Mon Mar 12, 2018 11:48 am

Did you know that 0 is an octal literal?
:)

User avatar
PeterO
Posts: 5879
Joined: Sun Jul 22, 2012 4:14 pm

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

Mon Mar 12, 2018 11:55 am

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

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

Mon Mar 12, 2018 12:28 pm

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.

User avatar
PeterO
Posts: 5879
Joined: Sun Jul 22, 2012 4:14 pm

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

Mon Mar 12, 2018 1:11 pm

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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

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

Mon Mar 12, 2018 1:21 pm

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!

Return to “Beginners”