Continue to Site

# Convert INT to CHAR in uVision

Status
Not open for further replies.

#### santinu

##### Newbie level 3
Hi
I am using uVision V3.30 to program the ATMEL 89C51AC2.

I am having a problem to convert char to integer numbers.

My problem is that i accept an input from a specific keypad as a character lets say '5' and i want to make certain calculations with this number.

I cannot calculate using a char variable therefore i have to convert it to an integer.

Using this command (int)(5) i get the ascii value of '5' not the actual number.

Can anyone help me out figuring what i should do so that the number taken as an input can be even used in calculations?

Thanks & Regards
Santinu

#### alexan_e

any char number (ascii) can be converted to an integer number if you subtract the number 48.

Alex

#### Sink0

##### Full Member level 6
Probably there is a function to make that, but for sure that function just have an ASC table or it subtracts 48 from the char... and then you can cast to int.

Something like this:

Code:
unsigned int get_number(char c)
{
if(c > 47)
return (unsigned int)(c-48);
else
return 0;
}

#### alexan_e

the ascii table is this

Code:
Decimal   Octal   Hex    Binary     Value
-------   -----   ---    ------     -----
[B]048[/B]      060    030   00110000        0
[B]049[/B]      061    031   00110001        1
[B]050[/B]      062    032   00110010        2
[B]051[/B]      063    033   00110011        3
[B]052[/B]      064    034   00110100        4
[B]053[/B]      065    035   00110101        5
[B]054[/B]      066    036   00110110        6
[B]055[/B]      067    037   00110111        7
[B]056[/B]      070    038   00111000        8
[B]057[/B]      071    039   00111001        9

as you can see the char '0' is decimal 48,
'1' is decimal 49 etc so in order for '1' which is 49 to become 1 you subtract decimal 48 so that 49 becomes 1.

Alex

#### santinu

##### Newbie level 3
But when i press 5 and is saved as character and i just subtract 48 i am subtructing from its ascii code already or do i have to convert that 5 to ascii code first then subtract??

#### alexan_e

A character is just a number, for example char '0' is the number 48, depending on the compiler you might need typecasting but usually you can just substract the number 48

Code C - [expand]1
2
3
4
5
6
7
char x;
unsigned int y;

x='0';  // x holds char 0 which is value 48
x=x-48;  // x becomes 0

y= (unsigned int)x-48;  //or use typecasting

Alex

#### santinu

##### Newbie level 3
View attachment Lcdkey.zipView attachment C program.txtView attachment schematic.zip

Here there are 3 files
the C program in a text file,
all the folder used for uVision
and the schematic in proteus

Long story short of the program. It accepts a value from 0 to 90 if it is in range it says good and if wrong it says that it is out of range.

Thanks & Regards
Santinu

#### alexan_e

what is the problem of the code?
How do you check the entered value, because there are also * / A B C D what happens if these are pressed or for value 8A or 9A.

Alex

#### Combinu

##### Newbie level 6
what is the problem of the code?
How do you check the entered value, because there are also * / A B C D what happens if these are pressed or for value 8A or 9A.

Alex

Hi i am helping in producing this code.. For now we are assuming to give the correct numbers only since the letters are going to be used for other processes... The problem with this code is that we need to check the valid range.

THanks

#### alexan_e

The code you have seems correct, doesn't it work?

Code C - [expand]1
2
3
4
5
6
7
t = (unsigned int)(deg[0]);
t = (t-48)*10;
u = (unsigned int)(deg[1]);
u = (u-48);
k = t+u;

if (k < 91) {}

The only problem would be if the input was '0' 'A' for example, in this case the result is still <91 so maybe you could check if each char is 0-9

Code C - [expand]1
2
3
4
5
6
7
8
9
10
if ((deg[0]>47) & (deg[0]<58) & (deg[1]>47) & (deg[1]<58)){  // check that both chars contain numbers (0-9) and not A,B,C etc

t = (unsigned int)(deg[0]);
t = (t-48)*10;
u = (unsigned int)(deg[1]);
u = (u-48);
k = t+u;

if (k < 91) {}
}

Alex

#### Combinu

##### Newbie level 6
No it does not work like this... And regarding if the input is 'A' that is not a problem for now since that is going to be removed... The problem is that if the input is say '56' with this code it still says out of range... It seems to be strange since the logic is ok!

#### alexan_e

Try it in an empty project and use the debugger to check because I think that this part if fine

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main(void){

volatile unsigned char deg[2];
volatile unsigned int t, u, k;

deg[0]='8';
deg[1]='9';

t = (unsigned int)(deg[0]);
t = (t-48)*10;
u = (unsigned int)(deg[1]);
u = (u-48);
k = t+u;

if (k < 91) {}

while(1);
}

then try it again with values like '9''5' and check the k content
Also note that if you go to the subtraction part and any of the chars is empty then the -48 result will be a big integer (below 0 it goes to 65535) so I think it would be a good idea to check if both char values are in range '0'-'9'.

Alex

#### uxmanz

##### Junior Member level 1
lets try an easy way.......and the best way....ascii is xxxx yyyy....where x is ur ascii identifier and yyyy is ur number........take input and take anl with 00001111.....it will leave ur number back!....or keyboard it works wont be needing 48s....or u can use datapointers DPTR.......ur choise

#### alexan_e

lets try an easy way.......and the best way....ascii is xxxx yyyy....where x is ur ascii identifier and yyyy is ur number........take input and take anl with 00001111.....it will leave ur number back!....or keyboard it works wont be needing 48s....or u can use datapointers DPTR.......ur choise

I wonder, if you read back what you wrote can you understand what you are saying because I certainly can't.
If you want to give a solution then give a full description without cut words and sentences, an example would help too.

Alex

#### uxmanz

##### Junior Member level 1
Sorry Alex for jarhead explanation...full explanation is below....it would help..i guess

in rows u can see...for example 2 ascii is 0011 0010 for 3 we have 0011 0011 for 4 we have 0011 0100...and so on up to 9.....for int we need only first nibble ...not the second one containing 0011...

for getting rid of this nibble or zeroing it we need to take andlogic (ANL) with 0000 1111
as shown in table....we will get following output

N ascii processed
0 00110000 00000000
1 00110001 00000001
2 00110010 00000010

it becomes integer....easily processable...
if i am on right track!

#### alexan_e

A keypad can't give this values, for example you can't get 0011 0011 , when you press a button you get only one enabled bit in X axis and one in Y axis (for example 0001 0010 ) and then you use a two dimensional array to represent it with a value.
You are probably using a pc keyboard which has a different way to send data.

Alex

#### uxmanz

##### Junior Member level 1
yea it was for pc keyboard.....any ways thanx Alex for correcting me~

#### andre_luis

##### Super Moderator
Staff member
santinu,

I didn´t analyse so deep the code posted for you, due big amount of files attached.
However, I had noticed that you´re accessing LCD.

Be aware that drift in mapping characters at some LCD chipsets are different from at ASCII; For example :
HD44780 -> 0(DEC) = 30(HEX)
T6963C -> 0(DEC) = 10(HEX)

Take a look on that possibility to be the root of your problem.

+++

Status
Not open for further replies.