convert string to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azrael
    New Member
    • Sep 2006
    • 1

    convert string to integer

    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...
  • sin
    New Member
    • Sep 2006
    • 13

    #2
    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;

    Comment

    • risby
      New Member
      • Sep 2006
      • 30

      #3
      Originally posted by sin
      You can filter all the numbers into another char[] and then do atoi();
      For example
      Code:
      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;
      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.
      Code:
      	 unsigned int i, ii;
      	    int result;
      	    char str1[] = "R12R21";
      	    char * str2 = malloc(strlen(str1));
      
      	if (str2 != NULL)
         {
      		memset(str2, 0, sizeof str1);
      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.

      Comment

      • neerajsathe
        New Member
        • Sep 2006
        • 2

        #4
        There is simple way around to achieve this....

        Code:
        char _szVal[] = "R12R21";
        int _a,_b ;
        sscanf(_szVal,"R%dR%d",&_a,&_b);

        Comment

        • sin
          New Member
          • Sep 2006
          • 13

          #5
          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

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by sin
            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.

            Comment

            • risby
              New Member
              • Sep 2006
              • 30

              #7
              Originally posted by Banfa
              Dynamic array allocation like that is a feature of the C99 standard.
              Neat. I didn't know that.

              Originally posted by 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
              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.

              Comment

              • sin
                New Member
                • Sep 2006
                • 13

                #8
                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[].

                Comment

                Working...