View topic - convert char to int

convert char to int

anything that doesn't fit to other groups.

convert char to int

Postby d1gger » Sat Jun 12, 2010 10:18 pm

Hi, I have very basic question how can I convert char to int ? a get value from PtText and I want convert char to int.
I get this way.

char *textp;
PtArg_t arg[1];

PtSetArg( &arg[0], Pt_ARG_TEXT_STRING, &textp, 0 );
PtGetResources( ABW_PtText1, 1, arg );

Thanks for any replay.
d1gger
New Member
 
Posts: 8
Joined: Thu May 27, 2010 7:52 pm

RE: convert char to int

Postby qnxloader » Mon Jun 14, 2010 4:50 am

int i=atoi(&textp)
qnxloader
Senior Member
 
Posts: 206
Joined: Mon Oct 06, 2003 8:29 am

Postby ingraham » Mon Jun 14, 2010 3:04 pm

While I like atoi for this example, I can't resist showing an alternate.
Code: Select all
int i=0;
...
if (sscanf(textp, "%d", i) !=1){
  // SOMETHING BAD HAPPENED, DEAL WITH IT.
}

-James Ingraham
Sage Automation, Inc.
ingraham
Senior Member
 
Posts: 161
Joined: Wed Nov 05, 2003 2:25 pm
Location: Texas

Postby JWR » Tue Jun 15, 2010 2:52 am

Code:

int i=0;
...
if (sscanf(textp, "%d", i) !=1){
// SOMETHING BAD HAPPENED, DEAL WITH IT.
}



Shouldn't that be sscanf(textp, "%d", &i) ? I thought the argument here must be a pointer, or have things changed since QNX 2?
JWR
New Member
 
Posts: 4
Joined: Fri May 04, 2007 4:52 am

Postby ingraham » Tue Jun 15, 2010 3:33 pm

Oops! You're right. &i is correct.
ingraham
Senior Member
 
Posts: 161
Joined: Wed Nov 05, 2003 2:25 pm
Location: Texas

Postby tfletche » Tue Jun 22, 2010 1:31 pm

Actually sscanf() is a bit heavy, I'm a fan to strtol() instead which allows you to convert different base values and also allows you to detect conversion errors.

Thomas
tfletche
QNX Master
 
Posts: 53
Joined: Wed Apr 11, 2007 11:19 am
Location: Ottawa


Return to General Programming

Who is online

Users browsing this forum: No registered users and 3 guests

cron