By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,871 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,871 IT Pros & Developers. It's quick & easy.

convert string to integer

P: 1
hi,

i have a string variable (e.g. R 005) and i like to convert it (numeric part only) to integer. how can i do that?

tnx...
Sep 21 '06 #1
Share this Question
Share on Google+
7 Replies


P: 13
sin
You can filter all the numbers into another char[] and then do atoi();
For example

int i, ii;
int result;
char str1[] = "R12R21";
char str2[strlen(str1)];

memset(str2, 0, sizeof str2);

for (ii = 0, i = 0; i < strlen(str1); i++)
if (isdigit(str1[i]))
str2[ii++] = str1[i];

result = atoi(str2);

----------------

You can't use atoi() without filtering because it is picking up numbers from beginning till any other char except number (ascii 48-57) occurs, so if first is 'R', the answer would be 0;
Sep 21 '06 #2

risby
P: 30
You can filter all the numbers into another char[] and then do atoi();
For example
Expand|Select|Wrap|Line Numbers
  1. int i, ii;
  2. int result;
  3. char str1[] = "R12R21";
  4. char str2[strlen(str1)];
  5.  
  6. memset(str2, 0, sizeof str2);
  7.  
  8. for (ii = 0, i = 0; i < strlen(str1); i++)
  9.     if (isdigit(str1[i]))
  10.         str2[ii++] = str1[i];
  11.  
  12. result = atoi(str2);
  13.  
----------------

You can't use atoi() without filtering because it is picking up numbers from beginning till any other char except number (ascii 48-57) occurs, so if first is 'R', the answer would be 0;
Unfortunately, you can't allocate str2 like that in C. You have to use malloc() to explicitly allocate memory at runtime (the compiler can't run the strlen function for you at compile time). You must then check that the allocation has worked before using the pointer.
Expand|Select|Wrap|Line Numbers
  1.      unsigned int i, ii;
  2.         int result;
  3.         char str1[] = "R12R21";
  4.         char * str2 = malloc(strlen(str1));
  5.  
  6.     if (str2 != NULL)
  7.    {
  8.         memset(str2, 0, sizeof str1);
  9.  
Also, if changed so that it compiles, this code will convert "R12R21" to
the integer value 1221 but surely the string contains two numeric values of 12 and 21.
Sep 22 '06 #3

P: 2
There is simple way around to achieve this....

Expand|Select|Wrap|Line Numbers
  1. char _szVal[] = "R12R21";
  2. int _a,_b ;
  3. sscanf(_szVal,"R%dR%d",&_a,&_b);
Sep 22 '06 #4

P: 13
sin
Risby, actually I can allocate str2 like this in C.
Did you tried that code ?
and it won't be 12 and 21
the result in str2 will be 1221
you can see that if you look closer in the program loop
Sep 23 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Risby, actually I can allocate str2 like this in C.
Did you tried that code ?
Dynamic array allocation like that is a feature of the C99 standard. Generally most people/compilers today still seem to stick to the C89 standard for some reason that is not clear to me.

Since this method of string allocation is also not compatible with C++ in my opinion it might be best avoided.

However like I said it is valid for C99.
Sep 23 '06 #6

risby
P: 30
Dynamic array allocation like that is a feature of the C99 standard.
Neat. I didn't know that.

Risby, actually I can allocate str2 like this in C.
Did you tried that code ?
and it won't be 12 and 21
the result in str2 will be 1221
you can see that if you look closer in the program loop
Steady on mate, I was only trying to be helpful.

Yes, I tried it, it gives error "Constant expression required in function main" with my ridiculously old fashioned compiler.

And yes, I know it won't be 12 and 21 but 1221. That was precisely what I was wondering about. I expect you couldn't read my post due to the steam coming out of your ears?

I'm assuming that a string with two sets of contiguous digits separated by a non-digit should be interpreted as two separate numeric values. It just seems odd to me that anyone in their right mind or any program would output a single four digit value with a great big R in the middle of it. I look forward to you enlightening me on the necessity for this.
Sep 23 '06 #7

P: 13
sin
When I read first post by azrael, I assumed that he wants to convert "numeric part only" to an interger (one integer).
Actually, there was a time when I needed precisely the same behaviour of program (pick all numbers from char[] and convert to int), so I gave an answer to him.
And there wasn't any steam from my ears at all =)
With gcc 3.3.5 I have no complains about that code. And I don't think that anyone must use exactly byte-by-byte code that is posted here.
The idea was to show that destination char[] must have at least same size as source char[].
Sep 25 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.