View previous topic :: View next topic |
Author |
Message |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
how to use printf for use ascii code |
Posted: Mon Feb 20, 2023 9:51 am |
|
|
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
|
|
Posted: Mon Feb 20, 2023 10:46 am |
|
|
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
|
|
Posted: Mon Feb 20, 2023 3:33 pm |
|
|
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
|
|
Posted: Tue Feb 21, 2023 12:24 am |
|
|
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
|
|
Posted: Tue Feb 21, 2023 5:28 pm |
|
|
my solution is:
Code: | #define code putc(68), putc(55), putc(1), putc(2), putc(38), putc(54);
...
code;
... |
|
|
|
|