d1gger
June 12, 2010, 10:18pm
1
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.
While I like atoi for this example, I can’t resist showing an alternate.
int i=0;
...
if (sscanf(textp, "%d", i) !=1){
// SOMETHING BAD HAPPENED, DEAL WITH IT.
}
-James Ingraham
Sage Automation, Inc.
JWR
June 15, 2010, 2:52am
4
Shouldn’t that be sscanf(textp, “%d”, &i) ? I thought the argument here must be a pointer, or have things changed since QNX 2?
Oops! You’re right. &i is correct.
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