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 support@ccsinfo.com

Convert '1' to 49, 'B' to 66 ...

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



Joined: 13 Nov 2006
Posts: 3

View user's profile Send private message

Convert '1' to 49, 'B' to 66 ...
PostPosted: Tue Dec 12, 2006 12:25 am     Reply with quote

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.

Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 12, 2006 1:34 am     Reply with quote

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

View user's profile Send private message

Convert '1' to 49, 'B' to 66 ...
PostPosted: Tue Dec 12, 2006 1:53 am     Reply with quote

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.
Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 12, 2006 2:17 am     Reply with quote

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







PostPosted: Tue Dec 12, 2006 3:36 am     Reply with quote

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







PostPosted: Tue Dec 12, 2006 6:02 am     Reply with quote

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
PostPosted: Fri Dec 15, 2006 6:45 am     Reply with quote

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

View user's profile Send private message

Converting
PostPosted: Fri Dec 15, 2006 9:40 pm     Reply with quote

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. Very Happy
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