View previous topic :: View next topic |
Author |
Message |
METsai
Joined: 13 Nov 2006 Posts: 3
|
Convert '1' to 49, 'B' to 66 ... |
Posted: Tue Dec 12, 2006 12:25 am |
|
|
Hi
Does anyone know how to convert ascii to number, ex: '1' to 49,
'D' to 68, 'i' to 105, 'u' to 117 ... Does CCS provide this function?
Or someone knows how to do that?
Thank you for your help.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 12, 2006 1:34 am |
|
|
Download the CCS manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Look in the section on the printf() function (page 196 in the Acrobat
reader). Look at the format specifiers. You want to display ASCII
values as decimal integers (or unsigned decimal integers). Which
format specifier listed in the CCS manual will allow you to do this ?
The answer is on that page. |
|
|
METsai
Joined: 13 Nov 2006 Posts: 3
|
Convert '1' to 49, 'B' to 66 ... |
Posted: Tue Dec 12, 2006 1:53 am |
|
|
Thank you for your help.
But I do not printf it.
I know printf can print ascii code,
char mystring;
mystring = 'D';
printf("mystring = %d", mystring);
>> mystring =68
But I want to use it. For example, I give PIC 'A', PIC converts 'A' to 65.
Then I give PIC 'g', PIC converts 'g' to 103. Then I can use 65, 103 for
more applications. Is it possible?
Thank you for your help.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 12, 2006 2:17 am |
|
|
Use the atoi() function. It's in the manual. You need to #include
the stdlib.h file in your program to use it. |
|
|
Zei Guest
|
|
Posted: Tue Dec 12, 2006 3:36 am |
|
|
I'm not sure if I understood this right but could you use just simple int8 number = 'A'; and number is now 65? |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 12, 2006 6:02 am |
|
|
Yes.
This is exactly the point. If you receive a character on the serial port for instance, what is stored in the processor, is just the number. It only 'becomes' the character, when you output it again.
So:
Code: |
//psuedo code only
int8 c;
c=getc();
printf("%c\n",c); //will display the character that arrived.
printf("%u\n",c); //will display the _value_ of the character that arrived.
//This is outputting up to three ASCII digits, _representing_ the number.
if (c=='A') {
printf("The character was 'A'\n");
//will print if the character is 'A'
}
if (c==65) {
printf("The character value is 65\n");
//will again print if the character is 'A'
}
if (c>64 && c<106) {
printf("The character is an upper case letter\n");
//if the ASCII value is between 65, and 105, then the character
//is A to Z.
printf("The lower case version is:%c\n",c-32);
}
|
The computer only works with the numbers themselves. There are 'shorthand' functions to help you, so if you type 'A', the compiler changes this to the number 65, and if you type a string of text, the right numbers to represent the string are generated. However what is store already _is_ the number, so as is shown in the last test, you can test for numbers, and perform arithmetic directly on the numeric value.
Best Wishes |
|
|
John676 Guest
|
Converting |
Posted: Fri Dec 15, 2006 6:45 am |
|
|
Hi,
Not 100% sure of what your doing, '1' is already 49.
If you mean how do you take '1' and turn it into "49" to output like that then:
char NumberToConvert;
char Result[3];
char Buffer;
NumberToConvert= '1'; // '1' = 49 = 0x31
Buffer = (NumberToConvert - (NumberToConvert % 100)) / 100;
Result[0] = Buffer + 0x30;
NumberToConvert = NumberToConvert - Buffer;
Buffer = (NumberToConvert - (NumberToConvert % 10)) / 10;
Result[1] = Buffer + 0x30;
NumberToConvert = NumberToConvert - (Buffer * 10);
Result[2] = NumberToConvert + 0x30;
// Result will have '0' and '4' and '9'
// another example
NumberToConvert= 'm'; // 'm' = 109 = 0x6D
Buffer = (NumberToConvert - (NumberToConvert % 100)) / 100;
Result[0] = Buffer + 0x30;
NumberToConvert = NumberToConvert - Buffer;
Buffer = (NumberToConvert - (NumberToConvert % 10)) / 10;
Result[1] = Buffer + 0x30;
NumberToConvert = NumberToConvert - (Buffer * 10);
Result[2] = NumberToConvert + 0x30;
// Result will have '1' and '0' and '9'
Let know if this is what you wanted.
NumberToConvert can be set to any value between 0 and 999. |
|
|
METsai
Joined: 13 Nov 2006 Posts: 3
|
Converting |
Posted: Fri Dec 15, 2006 9:40 pm |
|
|
Thank you for your help.
Your code helps me a lot.
But there is a strange situation.
When NumberToConvert is '0' ~ 'c', your code works fine.
When NumberToConvert is 'd', Result has '1' and '9' and '9'.
When NumberToConvert is 'e', Result has '1' and ':' and '0'.
When NumberToConvert is 'f', Result has '1' and ':' and '1'.
When NumberToConvert is 'g', Result has '1' and ':' and '2'.
.....
It seems NumberToConvert only can be set to any value between 0 and 99.
Could you help this problem?
Thank you very much. |
|
|
|