simple C question

Colt45

Lifer
Apr 18, 2001
19,720
1
0
I can't seem to think of how to do this, but there must be a simple way.

I've got an ADC coming in, dumping the value into a char.. so 0...255.

I need to take the value and split each decimal digit into its own char.

eg.
adc in = 123

char A = 1
char B = 2
char C = 3

I can't seem to think of an easy way to do it though?
 

Colt45

Lifer
Apr 18, 2001
19,720
1
0
I came up with this abortion.. seems to work.. not exactly pretty though

char_A = ADC_IN;
char_B = ADC_IN;
char_C = ADC_IN;
char_A /= 100;
char_B -= char_A * 100;
char_B /= 10;
char_C -= char_A * 100;
char_C -= char_B * 10;
char_C /= 1;
 

SoundTheSurrender

Diamond Member
Mar 13, 2005
3,126
0
0
Well as long as it works..
I actually don't use C, only been using C++ so I don't know.

Here's a idea.

char str[80] = "235";
(str) or (str + 0) points to the '2'
(str + 1) points to 1st '3'
(str + 2) points to 2nd '5'

Then you could throw it into a variable.

This is how I'd do it in C++, I think it would be similar in C..

 

Net

Golden Member
Aug 30, 2003
1,592
2
81
Okay, hopefully I didn't ready the post wrong.

here ya go:

#include <stdio.h>

int main(){

int num = 123, i, index = 3;
int num_chop[index];

for (i = (index-1); num != 0; i--){
num_chop = num%10;
num /= 10;
}

for (i = 0; i < index; i++){
printf ("\nnum_chop[%d]= %d", i, num_chop );
}

return 0;
}
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
num is the value to seperate into digits.
so num = 123;

printout looks like this

num_chop[0] = 1
num_chop[1] = 2
num_chop[2] = 3
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
here's how I'd do it. the whole approach really boils down to a sprintf(...) call. the rest of the code just shows you a way to dynamically find string length for any integral value.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
Colt45 what is the program for (ie intent or purpose).
there might be an even simpler approach.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
I'm not sure how exactly you're getting the input, but if it's from user input, you could just use:

scanf("%c%c%c", &a, &b, &c);

where a, b and c are defined as characters.

If you get it as an integer already entered, just do:

my_int = 123;
a = (my_int / 100) + 48;
b = ((my_int / 10) % 10) + 48;
c = (my_int % 10) + 48;

You mentioned something similar to this above, but it shouldn't work correctly, because a single-digit integer's value is not the same as its character representation. They're 48 off, which is why I added 48 to the characters in the value above. You can also simply add '0' (the character zero).
 

Colt45

Lifer
Apr 18, 2001
19,720
1
0
Its for a PIC microcontroller.

the compiler has a built in "adc_get" sort of routine. I pull the data from that,
then I split the value into the 3 pieces, read them off an array to change them to a 7-segment value instead of binary, and send them to 3 multiplexed 7 segment displays.

I'm using a pressure sensor from one of my car ECUs that blew up. So it will be a sort of atmospheric pressure / boost / vacuum gauge.


I guess I should have made it more clear originally. The code i've got now seems to function, but I managed to blow up the micro I originally wrote it for, so now i need to rebuild the circuit to use one with a different pinout :|
I knew using brown as +5v was going to come back to haunt me. hehe.
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
I would stick with the code you already have. The only change I would make would be to remove the divide by 1 at the end. It doesn't do anything other than waste CPU cycles.
 

Net

Golden Member
Aug 30, 2003
1,592
2
81
ah, reminds me of a electrical engineering class I had were we programed a 7 segment display on a fpga board. it was creating a stop watch.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |