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

ASCII to Hex

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



Joined: 01 Jun 2008
Posts: 1

View user's profile Send private message

ASCII to Hex
PostPosted: Sun Jun 01, 2008 9:31 am     Reply with quote

How do I convert to ANSII Hex?
Example:
ANSII = "ABC"
Hex = "41 42 43"
A = 41
B = 42
C = 43

In delphi I was to convert the conversion to decimal in the first after converting a Hex.
Ord ( "A") that function returns the code of the letter A
After having the cod convertia with the task IntToHex;
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Jun 01, 2008 11:11 am     Reply with quote

First it is ASCII American Standard Code for Information Interchange.
Second look at the PIC as an interpreter of information. For registers they have the ability to use a binary representation of a number directly. Registers can do binary arithmetic directly. Registers can do much more as long as code can interpret the value in a register. Now a letter like "A" can be encoded with a binary number to represent its value . Specifically for ASCII the decimal number 65 represented as binary is used. Now numbers can also be notated in hex in which case ASCII "A" is now 41. Now suppose "ABC" is read in via three successive statements vara=getc() varb=getc() varc=getc
vara has value of 65 decimal or 41 hex and notates for "A" in ASCII.
You question is unclear. Is it Delphi or CCS C you need help with?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Jun 02, 2008 7:02 am     Reply with quote

ASCII Characters are stored in memory as binary values. So a string defined as

Code:

char myString[4] = "ABC";

Will be stored as 01000001 (65 decimal, 0x41 Hex), 01000010 (66 decimal, 0x42 Hex) and 01000011 (67 decimal, 0x43 Hex) and not forgetting the terminating char 00000000 (0 decimal 0x00 Hex).


The representation of the value as binary, decimal or hex is only required when you come to display it or transmit the value as a series of ASCII characters.

Now the easiest way to do this on a PIC is to just print it:-
'A' has the value of 65, 0x41

Code:

printf("%c", 'A'); // Outputs an "A" on the serial port
printf("%X", 'A'); // Outputs "41" on the serial port
printf("%u", 'A'); // Outputs "65" on the serial port

printf("%c", 0x41); // Outputs an "A" on the serial port
printf("%X", 0x41); // Outputs "41" on the serial port
printf("%u", 0x41); // Outputs "65" on the serial port

printf("%c", 65); // Outputs an "A" on the serial port
printf("%X", 65); // Outputs "41" on the serial port
printf("%u", 65); // Outputs "65" on the serial port



If you have a string
Code:

char myString[4] = "ABC";

printf("%X%X%X", myString[0], myString[1], myString[2]); // outputs "414243"


Or you could use

Code:

char myString[4] = "ABC";
int i;

for(i = 0; i < strlen(myString; i++) {
  printf("%X", myString[i]);
}
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