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

RS232 printf - how do I send a hex value instead of ASCII
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
John_Lintern



Joined: 30 Sep 2004
Posts: 14

View user's profile Send private message Send e-mail

RS232 printf - how do I send a hex value instead of ASCII
PostPosted: Thu Apr 21, 2005 7:10 am     Reply with quote

I am trying to send the hex value 1B to an external LED display unit.

I've used the line...

printf ("%02X",0x1B);

I then monitored the RS232 comms using the Serial Port Monitor tool in CCS.

But instead of sending the hex value 1B, it sends the ASCII characters 1 and B (which is hex 31 and 42).

How do I make it send the actual hex value 1B ?

I am using a PIC16C74B running at 20MHz.

The baud rate is 1200, 8 bits, no parity.

Thanks.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 21, 2005 7:12 am     Reply with quote

Code:

putc(0x1b);
Guest








PostPosted: Fri Apr 22, 2005 5:31 am     Reply with quote

Code:

printf("%c",0x1b);
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 29, 2005 8:46 am     Reply with quote

printf ("%X",0x1B);
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 29, 2005 9:20 am     Reply with quote

lucky wrote:
printf ("%X",0x1B);


Didn't you read his post? That isn't what he wants and is what he did to begin with!
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 29, 2005 9:29 am     Reply with quote

I read it.

Code:
printf ("%X",0x1B);


will do as he wants.

Code:
printf ("%02X",0x1B);


however will not. This is what he had in the original post.

I use this in lots of products for PIC - GUI coms.
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 29, 2005 9:31 am     Reply with quote

lucky wrote:
I read it.

Code:
printf ("%X",0x1B);


will do as he wants.

Code:
printf ("%02X",0x1B);


however will not. This is what he had in the original post.

I use this in lots of products for PIC - GUI coms.


And he said
Quote:
But instead of sending the hex value 1B, it sends the ASCII characters 1 and B (which is hex 31 and 42).


He does not want the value to be converted to text. He just wants the value sent. A single char which in ascii would be the ESC key.
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 29, 2005 9:43 am     Reply with quote

Mark,

Code:

printf ("%02X",0x1B);


Will print the ASCII characters 1 and B. This is what he had.

Code:

printf ("%X",0x1B);


Will send the Single HEX byte 0x1B. This is what he wants to do.

Note the lack of '02' in my code post.
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Apr 29, 2005 9:51 am     Reply with quote

lucky wrote:
Mark,

Code:

printf ("%02X",0x1B);


Will print the ASCII characters 1 and B. This is what he had.

Code:

printf ("%X",0x1B);


Will send the Single HEX byte 0x1B. This is what he wants to do.

Note the lack of '02' in my code post.


Yeah, and pigs fly right. Nope wrong. It will convert it to text. The 02 will just force 2 chars like 0x0B would get printed as 0B where as in your case would just be B. Take a look in the manual as the difference between 'X' and 'x'. Note the difference being the output is either upper or lower case. Now that should make you think, hummm, numbers don't have case so Mark must be right Laughing
lucky



Joined: 12 Sep 2003
Posts: 46
Location: South Coast - England

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 29, 2005 10:03 am     Reply with quote

OK,

You are rite (as always). Embarassed

In feable defence, I was thinking of my debug code that does indeed print the Hex in character readable form.
_________________
Lucky
www.mpic3.com - MPIC3 player project, Forum, Downloads, Online Shop
Ttelmah
Guest







PostPosted: Sat Apr 30, 2005 7:43 am     Reply with quote

It is perhaps worth reflecting on the 'reason' for the slight confusion that did apply in this thread. This is simply that a 'Hex' vaue, is _inherently) ASCII!. Hex is a way of representing the raw binary value, using less ASCII characters, than the binary itself. The original poster, actually wanted to send the 'raw' value, represented by a pair of hex digits, not it's hex representation, but asked how to send a hex value.... Smile

Best Wishes
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Sat Apr 30, 2005 4:01 pm     Reply with quote

what about print a string like this in hexa format??

Code:
char test[]="Hi, This is a Test"
printf("%x",test);


Ist this posible?'

I want this string be printed in hexa format...


Tnahk you
Ttelmah
Guest







PostPosted: Sun May 01, 2005 2:27 am     Reply with quote

densimitre wrote:
wahat about print a string like this in hexa format??

Code:
char test[]="Hi, This is a Test"
printf("%x",test);


Ist this posible?'

I want this string be printed in hexa format...


Tnahk you

Remember that a 'string', is just an array of characters. So:
Code:

char test[]="Hi, This is a test";
int8 ctr;
for (ctr=0;ctr<strlen(test);ctr++)
   printf("%02x",test[ctr]);
putc('\n');

If you don't force each character to use two digits (the '02x' defintion), if you have a character with a leading '0', this will be dropped, and the receiver will get confused as to where it is in the string. I am sending a 'line feed' at the end of the string, but this is optional (up to you to work out how you want the other end to know you have reached the end of the string...).

You can also do the 'same', but instead of using strlen, simply take each character, test for it being '0' (the 'string terminator' character), and stop when this happens. So:
Code:

char test[]="Hi, This is a test";
int8 cval,ctr=0;
while(true) {
   cval=test[ctr++];
   if (cval==0) break;
   printf("%02x",cval);
}


Though this looks 'bulkier', it actually gives slightly smaller and quicker code, since the 'strlen' function, scans through the whole string to find the length, then the output scans the string again. This code combines the scan, and only performs one array access for each byte (array accesses like 'test[ctr]', are much slower than a direct variable access like 'cval').

Best Wishes
densimitre



Joined: 21 Dec 2004
Posts: 45

View user's profile Send private message

PostPosted: Sun May 01, 2005 8:56 am     Reply with quote

Hi, thank you for yor answers...

I did smilutae this code , and send only lower case character...what about ABDCDEF???

example:

Code:
char test[]="j";
int8 ctr;
for (ctr=0;ctr<strlen(test);ctr++)
   printf("%02x",test[ctr]);
putc('\n');


The output is:
Code:
6a

must be "6A", not "6a"


and changing "j" to "J":

Code:
char test[]="J";
int8 ctr;
for (ctr=0;ctr<strlen(test);ctr++)
   printf("%02x",test[ctr]);
putc('\n');


The outpu is:
Code:
4a

mut be "4A" not "4a"


What ist wrong???

Thank you
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Sun May 01, 2005 11:38 am     Reply with quote

Use a capital "X" instead of lowercase "x".

Go invest in a copy of "The C Programming Language", 2nd ed. by Kernighan and Ritchie. ISBN 0-13-110362-8
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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