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

Can you use printf to display binary digits?

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







Can you use printf to display binary digits?
PostPosted: Thu Nov 03, 2005 9:59 pm     Reply with quote

Hi there. I'm new to CCS, but I have quite a lot of C experience, as well as PIC experience (using PBP). I like working in C, but I'm wondering whether or not it's possible to somehow ammend printf so that you could say write:

Byte x = 255;
printf("%b", x);

And have that output "11111111". As in make a format command for binary representation of a number. I'm currently using an LCD, and am using printf with my LCD_Putc() function. I have made a seperate function called LCD_Putb() which outputs a single byte as a binary number. Is there anyway to somehow ammend printf so that a certain command (e.g. "%b") will make printf point to LCD_Putb() instead of LCD_Putc()? Is there some built-in way to represent binary numbers?

At the moment I can just write:

printf(LCD_Putc, "whatever %X", x);
LCD_Putb(y);

Which is fine, but I'd rather be able to use it from inside printf if possible, for readability. In PBP, binary numbers are builtin to the LCDOUT command.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 03, 2005 10:47 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=1523&highlight=printf+binary
forgie



Joined: 03 Nov 2005
Posts: 7

View user's profile Send private message

PostPosted: Fri Nov 04, 2005 1:04 am     Reply with quote

Um that thread is basically talking about doing what I've already done, which is make a function which displays a byte in binary form. What I want to know is can I amend the printf function somehow to add a formatting command which means 'binary'. It already has built-in formatting commands for hex and decimal numbers, I just want to add a formatting command for binary. Is it possible to change the way that PICC deals with printf?

edit: my function looks like this if anyone's interested
Code:

void lcd_putb( byte b)
{
   byte i;
   for (i = 0; i < 8; i++)
      if(bit_test(b, i))
         lcd_putc('1');
      else
         lcd_putc('0');
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Fri Nov 04, 2005 6:07 am     Reply with quote

forgie wrote:
Um that thread is basically talking about doing what I've already done, which is make a function which displays a byte in binary form. What I want to know is can a ammend the printf function somehow to add a formatting command which means 'binary'. It already has builtin formatting commands for hex and decimal numbers, I just want to add a formatting command for binary. Is it possible to change the way that PICC deals with printf?

edit: my function looks like this if anyone's interested
Code:

void lcd_putb( byte b)
{
   byte i;
   for (i = 0; i < 8; i++)
      if(bit_test(b, i))
         lcd_putc('1');
      else
         lcd_putc('0');
}

No, you would have to write your own printf
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