Like Tree1Likes

splitting integer into digits for PIC microcontroller -- newbe with C++

This is a discussion on splitting integer into digits for PIC microcontroller -- newbe with C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm new to C++. Years ago I dabbled in assembly lanquage as a hobby, but I never really got ...

  1. #1
    Registered User Mr. Quick's Avatar
    Join Date
    Oct 2013
    Location
    Maine
    Posts
    6

    splitting integer into digits for PIC microcontroller -- newbe with C++

    Hello,

    I'm new to C++. Years ago I dabbled in assembly lanquage as a hobby, but I never really got deep into manipulating numbers with it, so now that I'm learning C++, primarily for the purpose of microcontroller programming, I need to learn it.

    I'm working on a digital display circuit which will use multiple PIC microcontrollers. I need to be able to separate an integer into its digits for the ability to display them. Can someone help me here?

    The main PIC will sent the integer, but I need the controllers that are doing the actual 7 segment output to sepearate their appropriate numbers (for example, one PIC will be handling the ones and tens, and the second will be handling the hundreds and thousands). For reasons I won't get deep into right now, I am not using a single chip to multiplex the display.

    If someone can show me how to do this, and explain it a little, Im sure I will catch on.

    Thanks in advance!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,187
    I know a poor implementation of a brute force method which involves just diving by 10 a bunch and subtracting from your target integer but I know someone on this board posted a better implementation.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by MutantJohn View Post
    I know a poor implementation of a brute force method which involves just diving by 10 a bunch and subtracting from your target integer but I know someone on this board posted a better implementation.
    Hm? What does make digit extraction (achieved through division by base) a brute force algorithm?
    Elkvis likes this.
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User Mr. Quick's Avatar
    Join Date
    Oct 2013
    Location
    Maine
    Posts
    6
    Would either of you care to elaborate?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    right_digit = number%10;
    rest_of_it = number/10;
    In a loop, naturally.

  6. #6
    Registered User Mr. Quick's Avatar
    Join Date
    Oct 2013
    Location
    Maine
    Posts
    6
    OK, I was just reading up on the modulus operator, and it makes sense now. I wasn't familiar with it.

    So, in my project, I will have 2 PIC microcontrollers each handling 2 digits of a display.
    The one handling the thousands and hundreds, for example, will have the following
    Code:
    thousands_digit=num/1000
    remainder=num%1000
    hundreds_digit=num/1000
    Does that look right?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,420
    Quote Originally Posted by Mr. Quick View Post
    Does that look right?
    No.

    Given input (num) of 4356, it will calculate thousands_digit to be 4, remainder to be 356, hundreds_digit to be 4. I doubt that is quite what you are trying to achieve.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User Mr. Quick's Avatar
    Join Date
    Oct 2013
    Location
    Maine
    Posts
    6
    Oops, what I actually meant was
    Code:
    thousands_digit=num/1000
    remainder=num%1000
    hundreds_digit=remainder/100
    In your example, it should give me the hundreds_digit of 3, (with a remainder of 56 if I added the modulus to remainder)

    Correct?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,565
    Rather than asking, have you tried actually writing the code in a standard C++ compiler and testing it yourself?

    When I develop code for microcontrollers, it's not usual for me to test logic bits like this on a standard C compiler and see the results on the console before moving the code to the embedded source code.

  10. #10
    Registered User Mr. Quick's Avatar
    Join Date
    Oct 2013
    Location
    Maine
    Posts
    6
    I am actually just getting into this, and I've had a problem getting my IDE setup correctly, so I haven't been able to run the simulator.
    During the day, I'm at work and don't have access, so when I'm dabbling with ideas, the only thing for me to do is research it.
    I will definitely be simulating long before I write it to the chip.

    Thanks though.

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,187
    Yes, that code will do what you want it to do, assuming the system you're working on will interpret something some 123/10 to be 12.

    Let's consider 1234.

    1234%10 = 4. Check.
    1234/10 = 123.4, recast as an int this is 123
    123%10 = 3. Check.
    123/10 = 12.3, recase as an int this is 12
    12/10 = 2. Check.
    12/10 = 1.2, recast as an int this is 1
    1%10 = 1. Check.

    So we have 4, 3, 2, 1 extracted in that order.

    Edit: I'm not actually saying recast your code into floats but just that make sure your implementation supports these types of integer divisions. I'm not sure if that matters for embedded design or not.
    Last edited by MutantJohn; 11-05-2013 at 11:11 AM.

  12. #12
    Registered User Mr. Quick's Avatar
    Join Date
    Oct 2013
    Location
    Maine
    Posts
    6
    Maybe I'm misunderstanding the modulus operator. I thought the modulus operator gives you the remainder after dividing by the applied number.

    Code:
    num = 8765
    Remainder = num %1000
    Would assign the value of 765 to Remainder, where as
    Code:
    Thousands_digit = num / 1000
    would assign the value ot 8 to Thousands_digit.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, you're right. If you have a C compiler that doesn't treat integer divisions that way, what you actually have is a "C Compiler" in that it is something that claims to be a C compiler but isn't. The C language requires that (eg) 8765 / 1000 must give 8.

    EDIT: Forgot we were in the C++ forum. The same is true of the C++ language.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,420
    Quote Originally Posted by tabstop View Post
    Yes, you're right. If you have a C compiler that doesn't treat integer divisions that way, what you actually have is a "C Compiler" in that it is something that claims to be a C compiler but isn't. The C language requires that (eg) 8765 / 1000 must give 8.
    In C89, division by zero involving negative operands did not necessarily have a specified result. That changed with C99 (which specified that integer division rounds toward zero). The first C++ standard was compatible with C89.
    Right 98% of the time, and don't care about the other 3%.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by grumpy View Post
    In C89, division by zero involving negative operands did not necessarily have a specified result.
    I'm assuming the "by zero" in there is spurious?

    But in either standard, int / int was going to be an int and never a float.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of digits of an integer: Odd or Even?
    By Devolution in forum C Programming
    Replies: 14
    Last Post: 03-06-2009, 05:24 PM
  2. Integer digits
    By Necrofear in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2007, 08:38 AM
  3. Finding # of digits in an integer
    By Canucklehead in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2005, 12:54 AM
  4. Splitting integer
    By cyberCLoWn in forum C++ Programming
    Replies: 1
    Last Post: 12-27-2003, 05:08 PM
  5. Splitting Digits and Letters
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-08-2002, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21