View Full Version : Stupid question...
fraughton
04-20-2003, 03:57 AM
This may sound stupid but can math operations be done on a char in C? If not, can an int be either split into its individual digits or converted into a char so that I may split it?
I'm programming a pic16f84 to take a 12bit input and output to 4 7-segment displays. To do this I need to take the input, convert it to decimal, and then take each digit and run it through a switch statement to pick the proper output value to drive the display.
Obviously I'm a little rusty on the basics so any help is appreciated! Thanks.
A char really is just a small integer that holds ASCII representations.
'A' + 1 == 'B'
If you want to convert a character you know to be a digit into an integer of the same digit:
int equiv = thechar - 48;
i.e.
'0' - 48 == 0
'8' - 48 == 8
fraughton
04-20-2003, 04:32 AM
Ok, so could I do this:
char n = '20';
char m = n * 2;
or would I need to convert to an int first?
djdante97
04-20-2003, 05:20 AM
Yea you can do that. An int is (on an intel or motorolla) just 32-bits long where a char is 8 bits long (or 4 bytes for an int and 1 byte for a char). If you'd like to find out how big each data type is on your machine, print the result of a call to sizeof() in C.
eg :
printf("%d\n", sizeof(int));
fraughton
04-20-2003, 02:08 PM
Is '20' actually the number 20 or is it seen as two ASCII values?
If it sees it as the actual number then I now need to isolate the individual digits.
Say I've got:
char n= '2025';
I need to get:
char a1='2';
char a2='0';
char a3='2';
char a4='5';
How could I do this?
djdante97
04-20-2003, 06:36 PM
Originally posted by fraughton
Is '20' actually the number 20 or is it seen as two ASCII values?
My mistake, you can't assign '20' to a char. Anything in single quotes can only by 1 character long. '20' is seen as 2 ascii characters.
If it sees it as the actual number then I now need to isolate the individual digits.
Say I've got:
char n= '2025';
I need to get:
char a1='2';
char a2='0';
char a3='2';
char a4='5';
How could I do this?
So as i explained just above,
char n='2025';
won't work. you'll have to do something like:
int n=2025;
char a1=( (n%10000) /1000 ) + '0'; // gets 2, then converts it to ascii value to print '2'
char a2=( (n%1000) /100 ) + '0'; // so a2 = '0'
char a3=( (n%100) / 10 ) + '0'; // a3 gets '2'
char a4=( n%10 ) + '0'; // a4 gets '5'
if you have a char that contains the actual binary value of a digit from 0-9, adding '0' will return the ascii value for thata digit.
in the above example, you have to do a modulo so that you don't get any value greater than 9 when you do the division.
hope this helps a little.
fraughton
04-20-2003, 09:21 PM
I've got a solution that I think will work. Got it from the pcmech forum:
main(void)
{
unsigned short input; //12-bit word
unsigned char digit;
for(;;)
{
TRISA = 0xff; //Set Port A to input
TRISB = 0xff; //Set Port B to input
input = ((unsigned short)PORTA << 8) + (unsigned short)PORTB;
for (unsigned char i = 0; i < 4; i++)
{
digit = input%10;
input = input/10;
outputDigit(i,digit);
}
}
}
void outputDigit(unsigned char display, unsigned char digit)
{
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.