|
|
View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
printf with non printable characters |
Posted: Sun Dec 30, 2018 7:17 pm |
|
|
I need to send some variable fixed strings of about 15 to 25 bytes but I don't want to do this.
Code: |
printf(LoadTX2,"%c%c%c%c%c%c%c%c%c%c",0x00,0x02,0x03,0x25,0x00,0x04,0x00,0x14,0x00,0x36);
|
Because I must to count how many bytes are to know how many %c I must to use.
Also, as you see, there's some null bytes so I think I can't use a string.
On the other hand I need to send a 24 Bit ID
Code: |
int32 myID = 0x078090;
|
How I should add that variable to the printf? _________________ Electric Blue |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 30, 2018 7:30 pm |
|
|
Quote: | I need to send some variable fixed strings of about 15 to 25 bytes |
Put the bytes in an array and use putc() in a for() loop to send them.
Use sizeof on the array in the for() loop to get the number of bytes.
Quote: | On the other hand I need to send a 24 Bit ID
int32 myID = 0x078090;
How I should add that variable to the printf? |
Use %lx to send it with printf. Or, tell us what format you want to
send it in. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Sun Dec 30, 2018 7:39 pm |
|
|
I need to send the int32 0x078090 as literal bytes 0x07,0x80,0x90 not as ASCII string.
All this is a security protocol between two processors, so there's non ASCII printable characters.
As for the array, as far I understand, sizeof retrieves the size of the array not the actual byte count; so I guess I must to create a new variable for each string of bytes. Am I correct? _________________ Electric Blue |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 30, 2018 7:53 pm |
|
|
This would do it:
Code: |
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//======================================
void main()
{
int8 data[]= {0x00,0x02,0x03,0x25,0x00,0x04,0x00,0x14,0x00,0x36};
int32 myID = 0x078090;
int8 i;
for(i=0; i < sizeof(data); i++)
putc(data[i]);
putc(myID >> 16);
putc(myID >> 8);
putc(myID);
while(TRUE);
} |
|
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Sun Dec 30, 2018 8:11 pm |
|
|
Thanks, that help me with a lots of doubts. _________________ Electric Blue |
|
|
|
|
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
|