CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How does one convert a byte to a char?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Spero
Guest







How does one convert a byte to a char?
PostPosted: Tue Jul 12, 2005 10:50 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 12, 2005 10:57 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 12, 2005 12:50 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Jul 13, 2005 5:49 am     Reply with quote

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!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group