View previous topic :: View next topic |
Author |
Message |
Spero Guest
|
How does one convert a byte to a char? |
Posted: Tue Jul 12, 2005 10:50 am |
|
|
Does anyone know if there any built-in function that I can use to convert a byte into a char? I am reading the time & date from a DS1307 RTC IC (BCD format) and displaying it on a character LCD screen, using the standard routines from LCD.C
Currently I am using a routine that accepts the byte value form the DS1307 and returns the equivalent char ready for displaying; see below.
char convert (byte digit) {
char c;
switch (digit) {
case 0 : c = '0'; break;
case 1 : c = '1'; break;
case 2 : c = '2'; break;
case 3 : c = '3'; break;
case 4 : c = '4'; break;
case 5 : c = '5'; break;
case 6 : c = '6'; break;
case 7 : c = '7'; break;
case 8 : c = '8'; break;
case 9 : c = '9'; break;
}
return(c);
}
Thanks,
Spero. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jul 12, 2005 10:57 am |
|
|
I would use sprintf OR if you want quick and dirty,...
check that your data is valid and add 0x30; (48) |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Tue Jul 12, 2005 12:50 pm |
|
|
If you mean an 8-bit value into its ASCII character then just add 0x30 after checking that the 8-bit value is in the range 0 to 9.
Code: |
// assuming mybyte is SIGNED
if (mybyte >= 0 && mybyte < 10) mychar = 0x30 + mybyte;
// assuming mybyte is UNSIGNED
(if mybyte < 10) mychar = 0x30 + mybyte;
|
If you mean 8-bit data type to char data type, the char data type is 8-bits on the PIC, and I believe it is unsigned by default. So there is no data type conversion necessary for byte to char. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Wed Jul 13, 2005 5:49 am |
|
|
Same thing but a little easier to read..
// assuming mybyte is SIGNED
if (mybyte >= 0 && mybyte < 10) mychar += '0';
// assuming mybyte is UNSIGNED
(if mybyte < 10) mychar += '0'; _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
|