# 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. ## 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.

2. 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. Originally Posted by MutantJohn
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?

4. Would either of you care to elaborate?

5. Code:
```right_digit = number%10;
rest_of_it = number/10;```
In a loop, naturally.

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. Originally Posted by Mr. Quick
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.

8. 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. 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. 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. 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.

12. 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. 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. Originally Posted by tabstop
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.

15. Originally Posted by grumpy
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 Last