Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Select Variable Digit

Status
Not open for further replies.

tom_pay

Member
Hi Everyone,

I have a decimal variable, and I want to select one of the digits, then the next and so fourth. How do I do this?

Say the value of the variable is 243, I would like to get a 2, a 4 and a 3.

I am using the C programming language on a PIC 16F684.

It has really got me stumped.

Thanks

Tom
 
That's not so hard. First divide your number by 100 and ignore the remainder (easy to do in programming) - there's your 2. Subtract that 2*100 from the number. Then divide by 10 and ignore remainder - there's your 4. Subtract 4*10 and what's left is your 3.

I vaguely remember the first time I figured this out for myself. I puzzled over it for hours. Then it turned out to be pretty easy.

There are fancy, complicated algorithms to do it too, but my simple way works pretty well if you don't need big speed. You're likely doing it for a display, right? No speed needed there.
 
Last edited:
OK,

That is so simple.

It is for a 7 segment display, so the divisions wont be a problem.

Thanks so much.

Tom
 
A slightly different approach. Assuming your number is abc then,
Code:
    c=num%10;
    num/=10;
    b=num%10;
    a=num/10;

Mike.
 
Last edited:
Hi

I have tried both bits of code and they both work well.

#1
ones = number % 10;
number /= 10;
tens = number % 10;
hundreds = number / 10;

and

#2
hundreds = number / 100;
tens = (number - (hundreds * 100) ) / 10;
ones = number - (hundreds * 100 + tens * 10);

Whilst they both work fine I found some differences:

1st Code 2nd Code
Program Space 17.7% 16.9%
Data Space 20.3% 26.6%
Time to Execute 506.2µs 299µs

Since they are both quick enough for the purpose. I don't know which to use. This then poses a problem.

Which is more valuable, Program Space or Data Space?


Thanks


Tom
 
Status
Not open for further replies.

Latest threads

Back
Top