Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jun 02, 2008 7:02 am |
|
|
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]);
}
|
|
|