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 CCS Technical Support

Converting Char into int

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



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

Converting Char into int
PostPosted: Sun Mar 26, 2017 7:11 pm     Reply with quote

Why not working using ?

int k;
Char Mystring[20];

Mystring = "12345"

k = Mystring[0];

if (k == 1) Putc('O');

its work only if use 49(asc code) in this case if (k == 49) Putc('O');
drolleman



Joined: 03 Feb 2011
Posts: 116

View user's profile Send private message

PostPosted: Sun Mar 26, 2017 7:27 pm     Reply with quote

first are you using a 16/18 part because int is only 8 bits 0 - 255

int in pic24 or pic33 is 16 bit 0 - 65535.

to get the data would be k= atoi(Mystring),
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 26, 2017 8:13 pm     Reply with quote

The string is ASCII characters. Therefore, do the comparison to an
ASCII character. This means compare it to '1' (and not to 1). They are
different. Do it like this:
Code:
if (k == '1') Putc('O');
jpts



Joined: 08 Mar 2017
Posts: 40

View user's profile Send private message

PostPosted: Mon Mar 27, 2017 7:09 pm     Reply with quote

worked using if (k == '1') Putc('O');

but how to convert char into int8. once atoi only work with string.

Take same example.

int k,y;
Char Mystring[20];

Mystring = "12345"

k = Mystring[0];

y = 2 + k; ????? for correct result should convert k into int ? toget y = 3 as result
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 27, 2017 8:54 pm     Reply with quote

Use the toint() macro as shown in the example below.
The program will display the output as:
Quote:

1

Code:
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)


// This macro converts an ASCII hex digit from '0' to 'F'
// to an integer from 0x00 to 0x0F (0 to 15).
#define toint(c)  ((c & 0x5F) > '9' ? c - '7' : c - '0')

//======================================
void main()
{                     
int8 k;
int8 Mystring[20] = "12345";

k = toint(Mystring[0]);

printf("%u ", k);

while(TRUE);
}
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