Printf Question

This is probably a dumb question but I dont have as much experience with C as I’d like to :slight_smile: Is it possible to printf a char as an ASCII character? printf_lib.c says %c is unsupported. But somehow strings enclosed in quotation marks print with no problems. How do I convert a byte to a single-character string?

try creating a 1-element char array, and then assigning the value of the byte to the first element (element 0) in the array.

Unfortuately, this does not seem to work. I get some garbage as output and then the RC locks up. Could you post some example code?

And, actually, this is not really critical :slight_smile: Its ok if I dont get it working.

last i looked, there were functions to output individual bytes and other things to the output window… try the PrintString() function defined in ifi_utilities.h/c

If you read the file “printf_lib.c” (I think) you will see a comment that says “%c” is not supported. The best you are going to do is put the character into a char array two long setting your character in location 0 and ‘\0’ (or zer0) in location 1, then print it using “%s” as the format and using the array name.

char t [2];
t [0] = 61; // Or what ever you character is.
t [1] = ‘\0’ // Null character.
printf ("%s
", t);

yeah, try that… forgot about the null terminator, because i’m so used to just using strcpy :slight_smile: gotten lax.

I actually figured this one out on my own last night :slight_smile: Sorry forgot to post here.

What was the fix, anyway? I like to know so that if I ever get it, I have an idea on what it might be.

Actually, the byte array thing worked on MS VC6.0 but didnt in MPLAB :slight_smile: One way of sending an unsigned char as an ascii character is actually very simple. I just do what printf_lib.c does when sending a byte - assign the unsigned char to the transmit register (I think it is called TXREG) and then call WaitForTXEmpty.

Tricky. :slight_smile: That interesting. Thanks for that info.