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 to use printf for use ascii code

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



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

how to use printf for use ascii code
PostPosted: Mon Feb 20, 2023 9:51 am     Reply with quote

I want to send on UART code in hex like:
printf(1,2,3)
result to be in hex:
0x01 0X02 0X03
or
1 2 3
I can use command putc()
putc (1);
putc (2);
putc (3);

and result is:
123 (is okay but only single constant character on row I can put...)

question is how to use single and simple function to print constant character?

thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19497

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 10:46 am     Reply with quote

Code:

   int val;
   for( val=1;val<4;val++)
       printf("0x%02x ", val);


Keys are, the '0x', is explicitly added, then you use %02X to print a value
as a hexadecimal (upper case when over 9), result, using 2 digits, with a
leading zero if needed.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon Feb 20, 2023 3:33 pm     Reply with quote

Thank you for reply Ttelmah
but code
Code:
printf("%02x%02x%02x",1, 2, 3);

result is

30 31 30 32 30 33 and so on. (ascii code)

I need result like

1 2 3

or something like

01 02 03 (ascii code)

made by
Code:
putc(1);
putc(2);
putc(3);
Ttelmah



Joined: 11 Mar 2010
Posts: 19497

View user's profile Send private message

PostPosted: Tue Feb 21, 2023 12:24 am     Reply with quote

It is not sending what you say. You are using a program that displays
ASCII, which is why you are seeing it as ASCII. If you want to see a raw
character, then use %c. So:
Code:

printf("%c%c%c",1, 2, 3);


This sends the value as a raw character. This will then display in ASCII as
01 02 03

(depending on how the program you are using shows the characters).
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Tue Feb 21, 2023 5:28 pm     Reply with quote

my solution is:

Code:
#define code putc(68), putc(55), putc(1), putc(2), putc(38), putc(54);

...
code;
...
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