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

Problem with converting ASCII to int

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



Joined: 04 Feb 2020
Posts: 18

View user's profile Send private message

Problem with converting ASCII to int
PostPosted: Tue Feb 04, 2020 10:13 pm     Reply with quote

Hi guys,

I have a problem with the conversion. On the output of port C I receive only "128".

I am using:
PIC16F690
CCS PIC C 5.82
Windows 10 Pro

Code:

unsigned int8 CharToInt (char cInput)
{
   unsigned int8 i=0;
   const char cCharCompare [17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', '#', '*', '\0'};
   for (i=0; i<16; i++)
   {
      if (cInput == cCharCompare[i])
         return i;
      else
         return 128;
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 04, 2020 10:43 pm     Reply with quote

The logic of your routine is incorrect. In your version, if it gets a mismatch
on the very first test, then it bails with error code (128). You don't
want that. You only it want it to exit with an error if it checks all the
values in the array and it got no matches at all.

Fixed version:
Code:
unsigned int8 CharToInt (char cInput)
{
unsigned int8 i=0;
char cCharCompare [17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', '#', '*', '\0'};

for(i=0; i<16; i++)
   {
    if(cInput == cCharCompare[i])
       return i;
   }
return(128);
}
RamShop55



Joined: 04 Feb 2020
Posts: 18

View user's profile Send private message

SOLVED
PostPosted: Wed Feb 05, 2020 4:56 am     Reply with quote

Thanks, a noob mistake from my side. I do too much and sometimes its hard to notice the obvious.
temtronic



Joined: 01 Jul 2010
Posts: 9101
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 05, 2020 5:53 am     Reply with quote

just a comment.

in CCS C, an 'unsigned int8' IS a 'char'.

That can save you a LOT of typing and time.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19218

View user's profile Send private message

PostPosted: Wed Feb 05, 2020 8:05 am     Reply with quote

Though not if you go to the PIC24/30/33, where a char is signed.
This sort of difference is why I prefer to load stdint.h, and then explicitly
use uint8_t, int8_t etc..
Saves so much grief when you move 'up' the chip families!.
temtronic



Joined: 01 Jul 2010
Posts: 9101
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 05, 2020 9:20 am     Reply with quote

yeah, I'm too old to learn NEW PICs.... Laughing
I had enough problems going from 16C84 to 18F46K22 !
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