|
|
View previous topic :: View next topic |
Author |
Message |
RamShop55
Joined: 04 Feb 2020 Posts: 18
|
Problem with converting ASCII to int |
Posted: Tue Feb 04, 2020 10:13 pm |
|
|
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
|
|
Posted: Tue Feb 04, 2020 10:43 pm |
|
|
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
|
SOLVED |
Posted: Wed Feb 05, 2020 4:56 am |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Feb 05, 2020 5:53 am |
|
|
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: 19513
|
|
Posted: Wed Feb 05, 2020 8:05 am |
|
|
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: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Feb 05, 2020 9:20 am |
|
|
yeah, I'm too old to learn NEW PICs....
I had enough problems going from 16C84 to 18F46K22 ! |
|
|
|
|
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
|