Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Printf Question (http://www.chiefdelphi.com/forums/showthread.php?t=26443)

Greg 06-03-2004 19:17

Printf Question
 
This is probably a dumb question but I dont have as much experience with C as I'd like to :) 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?

deltacoder1020 06-03-2004 19:23

Re: Printf Question
 
Quote:

Originally Posted by Greg
This is probably a dumb question but I dont have as much experience with C as I'd like to :) 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.

Greg 06-03-2004 20:12

Re: Printf Question
 
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 :) Its ok if I dont get it working.

deltacoder1020 06-03-2004 22:10

Re: Printf Question
 
Quote:

Originally Posted by Greg
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 :) 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

Daniel 06-03-2004 22:59

Re: Printf Question
 
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\n", t);

deltacoder1020 07-03-2004 00:22

Re: Printf Question
 
Quote:

Originally Posted by Daniel
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\n", t);

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

Greg 07-03-2004 13:43

Re: Printf Question
 
I actually figured this one out on my own last night :) Sorry forgot to post here.

Ryan M. 07-03-2004 21:24

Re: Printf Question
 
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.

Greg 08-03-2004 01:06

Re: Printf Question
 
Actually, the byte array thing worked on MS VC6.0 but didnt in MPLAB :) 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.

Ryan M. 08-03-2004 06:57

Re: Printf Question
 
Quote:

Originally Posted by Greg
Actually, the byte array thing worked on MS VC6.0 but didnt in MPLAB :) 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. :) That interesting. Thanks for that info.


All times are GMT -5. The time now is 02:51.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi