How to Convert a CHAR to an workable INT number

Hi Guys,
I am busy with a project of mine but are stuck. Currently I'm entering a value on a keyboar (1-9) on my TFT touch, which will act as a "set value" function on my project.
This 3 number value will then be compared to an analog number and a desired function will be done thereon.
However, the number that I'm displaying is a Character number, which I need to convert to a Integer.
Ex. I have a CHAR Diaplay value ["1", "2", "3"]

which I need to convert to a Int Value = 123

I have tried many methods, including atol an atoi, Mabe I'm applying it incorrectly.

Below is my code, currently when converting the Char to Int it only returns a "0" value.

include <UTFT.h>
#include <URTouch.h>

UTFT myGLCD(ITDB28,A5,A4,A3,A2);

//
URTouch myTouch( A1, 10, A0, 8, 9);

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SmallFont[];
extern uint8_t SevenSegNumFont[];

int x, y;
char stCurrent[3]="";
int stCurrentLen=0;
char stLast[3]="";

/*************************
** Custom functions **
*************************/
void serial() {

Serial.begin(9600);

}

void drawButtons()
{

// Draw the upper row of buttons
for (x=0; x<5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x60), 10, 60+(x60), 60);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x60), 10, 60+(x60), 60);
myGLCD.printNumI(x+1, 27+(x60), 27);
}
// Draw the center row of buttons
for (x=0; x<4; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10+(x
60), 70, 60+(x60), 120);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10+(x
60), 70, 60+(x60), 120);
myGLCD.printNumI(x+6, 27+(x
60), 87);

myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (250, 70, 300, 120);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (250, 70, 300, 120);
myGLCD.printNumI(0, 267,87);

myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (10, 130, 150, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 130, 150, 180);
myGLCD.print("CLEAR", 40, 147);

myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (160, 130, 300, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (160, 130, 300, 180);
myGLCD.print("ENTER", 190, 147);
myGLCD.setColor(VGA_RED);
myGLCD.setFont(BigFont);
myGLCD.print("KPA", 220, 220);

}

}

void updateStr(int val)
{
if (stCurrentLen<3)
{

myGLCD.setFont(SevenSegNumFont);
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, CENTER, 185);
myGLCD.setColor(255, 0, 0);
myGLCD.setColor(0, 255, 0);

int pos = atoi(&stCurrent[3]);
int disp = pos;

myGLCD.setColor(0, 255, 0);
myGLCD.printNumI(disp, LEFT, 185);
myGLCD.setColor(255, 0, 0);
myGLCD.setColor(0, 255, 0);

Serial.print(disp);
}

}

// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}

/*************************
** Required functions **
*************************/

void setup()
{

// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();

myTouch.InitTouch();
myTouch.setPrecision(PREC_HI);

myGLCD.setFont(BigFont);
myGLCD.setBackColor(0, 0, 255);
drawButtons();

}

void loop()
{
while (true)
{
if (myTouch.dataAvailable())
{
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();

if ((y>=10) && (y<=60)) // Upper row
{
if ((x>=10) && (x<=60)) // Button: 1
{
waitForIt(10, 10, 60, 60);
delay(200);
updateStr('1');
int z = 1;
}
if ((x>=70) && (x<=120)) // Button: 2
{
waitForIt(70, 10, 120, 60);
delay(200);
updateStr('2');
}
if ((x>=130) && (x<=180)) // Button: 3
{
waitForIt(130, 10, 180, 60);
delay(200);
updateStr('3');
}
if ((x>=190) && (x<=240)) // Button: 4
{
waitForIt(190, 10, 240, 60);
delay(200);
updateStr('4');
}
if ((x>=250) && (x<=300)) // Button: 5
{
waitForIt(250, 10, 300, 60);
delay(200);
updateStr('5');
}
}

if ((y>=70) && (y<=120)) // Center row
{
if ((x>=10) && (x<=60)) // Button: 6
{
waitForIt(10, 70, 60, 120);
delay(200);
updateStr('6');
}
if ((x>=70) && (x<=120)) // Button: 7
{
waitForIt(70, 70, 120, 120);
delay(200);
updateStr('7');
}
if ((x>=130) && (x<=180)) // Button: 8
{
waitForIt(130, 70, 180, 120);
delay(200);
updateStr('8');
}
if ((x>=190) && (x<=240)) // Button: 9
{
waitForIt(190, 70, 240, 120);
delay(200);
updateStr('9');
}
if ((x>=250) && (x<=300)) // Button: 0
{
waitForIt(250, 70, 300, 120);
delay(200);
updateStr('0');
}
}

if ((y>=130) && (y<=180)) // Upper row
{
if ((x>=10) && (x<=150)) // Button: Clear
{
waitForIt(10, 130, 150, 180);
delay(200);
stCurrent[0]='\0';
stCurrentLen=0;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(0, 181, 220, 250);

}
}
}
}

This is the part I'm refering to,

myGLCD.setFont(SevenSegNumFont);
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, CENTER, 185);
myGLCD.setColor(255, 0, 0);
myGLCD.setColor(0, 255, 0);

int pos = atoi(&stCurrent[3]);
int disp = pos;

atoi works on correctly terminated C strings.
Have you got a correctly terminated C string?

Please remember to use code tags when posting code

char stCurrent[3]="";

    int pos = atoi(&stCurrent[3]);

The valid index values for stCurrent are 0, 1, and 2. Three is NOT a valid index.

The atoi() function expects a NULL terminated array of chars, NOT a single char.

Your array can hold two digits and a NULL.

@AWOL, I assume it is Terminated, as the Display functions correctly.
When I press the button called "1" it displays it correctly, when I press "2" it will add the "2" correctly to the String to form "12" etc. etc.

What I'm trying to achieve is for example, I will enter the value 220, by pressing "2" and "2" and "0" it displays the String correctly as 220, however I can get this Char String converted to an integer of 220 to use in my back program calculations.

@PaulS, If I change index value to "0" I get the error " initializer-string for array of chars is too long[-fpermissive]
For the index "1" and "2" my program simply doesn't respond to the inputs

Thank you for you assistance, I appreciate it, please not that I'm relatively new to programming.

As Paul pointed out, if you're storing 3 digits and a terminating null character, this is not long enough:-char stCurrent[3]="";then, if you want to convert the result to an int, you need to pass the address of the string, not the address of a single character, element #3, (which doesn't exist).
So instead of this:-int pos = atoi(&stCurrent[3]);you'd want this:-int pos = atoi(stCurrent);

willerpw:
@AWOL, I assume it is Terminated, as the Display functions correctly.
When I press the button called "1" it displays it correctly, when I press "2" it will add the "2" correctly to the String to form "12" etc. etc.

I almost never assume.
You're talking about two different entities (String and string), and as has already been noted, you don't own the fourth element of a three element array, so it's a dangerous thing to use.

Hi Guys, thank you for your input.

I't is working fine with minor glitches which I will address. I took note and implemented code " int pos = atoi(stCurrent);" , I assume its working :slight_smile:

Atleast I can carry on until my next hurdle.

Thank you again

willerpw:
Hi Guys, thank you for your input.

I't is working fine with minor glitches which I will address. I took note and implemented code " int pos = atoi(stCurrent);" , I assume its working :slight_smile:

Atleast I can carry on until my next hurdle.

Thank you again

I hope you also changed this:-char stCurrent[3]="";to this:-char stCurrent[4]="";

If you have a single character, the idiom

int i = c - '0';

is probably the best.

KeithRB:
If you have a single character, the idiom

int i = c - '0';

is probably the best.

It would be, but it's 3 characters/digits:-

willerpw:
Ex. I have a CHAR Diaplay value ["1", "2", "3"]
which I need to convert to a Int Value = 123

And it's really "123", not "1", "2", "3"

OldSteve:
It would be, but it's 3 characters/digits:-And it's really "123", not "1", "2", "3"

so, multiply each digit by its decimal weight.

I read it as '1', '2', '3'.

AWOL:
so, multiply each digit by its decimal weight.

I read it as '1', '2', '3'.

Not in his code, which is what I was referring to when I said "And it's really "123" ". He calls the function like this, passing 'val' as the parameter:-updateStr('3');
Then in the function:-

stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;

which results in "123" after 3 calls to the function.
Edit: Assuming the values passed were '1', '2' and '3', of course.

But that's not to say that it wouldn't be more efficient to do it as you say, subtracting '0' for each character, then multiplying by weight.
I was just going with his current 'updateStr()' function.