transfer from ASCII to int

Q

QQ

Hello I have a string
"12345" for example,
I'd like to change it to put the first n digits(123 for example) into
int a=123,
the 4,5 digits to be b=45.

Now I am using atoi.
I parse the string "12345" first to be s1,s2,
and then change it to be a=atoi(s1) b=atoi(s2)
Any better way for it?
Thanks a lot!
 
M

Michael Mair

QQ said:
Hello I have a string
"12345" for example,
I'd like to change it to put the first n digits(123 for example) into
int a=123,
the 4,5 digits to be b=45.

Now I am using atoi.
I parse the string "12345" first to be s1,s2,
and then change it to be a=atoi(s1) b=atoi(s2)
Any better way for it?
Thanks a lot!

Give us code, please.
Your description is not as clear as well-written code and
may hide errors.

It is almost always a bad idea to use atoi() as there is
an error-checking function which does the same job and
reports errors: strtol()

BTW: You need only to strcpy() once as the second atoi()
/strtol() call can be passed str+n where str is a pointer
to the string.

If n is constant, you also can abuse sscanf() to perform
the job:
e.g. my_n = 3:
ret = sscanf(str, "%3d%d", &a, &b);
if (ret != 2) {
/* maybe it is time for checking strlen(str) and some error
** handling */
}
(untested)
or, with more flexibility and less readability
#define MY_N 3
#define stringize(s) #s
#define XSTR(s) stringize(s)

....
ret = sscanf(str, "%" XSTR(MY_N) "d%d", &a, &b);
....
(untested, too)


Cheers
Michael
 
C

CBFalconer

Michael said:
.... snip ...

If n is constant, you also can abuse sscanf() to perform
the job:
e.g. my_n = 3:
ret = sscanf(str, "%3d%d", &a, &b);
if (ret != 2) {
/* maybe it is time for checking strlen(str) and some error
** handling */
}
(untested)
or, with more flexibility and less readability
#define MY_N 3
#define stringize(s) #s
#define XSTR(s) stringize(s)

....
ret = sscanf(str, "%" XSTR(MY_N) "d%d", &a, &b);
....
(untested, too)

Why fool about with any flavor of scanf. Try (untested):

/* a non-digit ends the field */
unsigned int usefield(const char *s, int n)
{
unsigned int value = 0;

while (n-- && isdigit((unsigned char)*s))
value = 10 * value + (*s++ - '0');
return value;
} /* usefield untested */

Don't forget to #include <ctype.h>. Now the OP can do:

a = usefield(str, 3);
b = usefield(str+3, 2);

You can gussy it up to absorb leading blanks if you wish.
 
A

Aaron Gage

QQ said:
Hello I have a string
"12345" for example,
I'd like to change it to put the first n digits(123 for example) into
int a=123,
the 4,5 digits to be b=45.

Now I am using atoi.
I parse the string "12345" first to be s1,s2,
and then change it to be a=atoi(s1) b=atoi(s2)
Any better way for it?
Thanks a lot!


try (untested):

int N = ??, i, a, b;

char cpy[???];
char input[???];

gets(input);

// copy 1st N chars
strncpy(cpy, input, N);
a = atoi(cpy);

// copy remaining chars
strncpy(cpy, input+N, strlen(input)-N);
b = atoi(cpy);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top