Convert two char into one int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mariano

    Convert two char into one int

    I have this lines:

    char *chr;
    int integ;
    num[0] = '1';
    num[1] = '9';

    How I could obtain value 19 in the integ variable????
    Hi to all.
  • Kenny McCormack

    #2
    Re: Convert two char into one int

    In article <8a82a41a-1992-4fa3-9c9b-bc4f6c2b3d6b@e3 9g2000hsf.googl egroups.com>,
    Mariano <mariano.caland ra@gmail.comwro te:
    >I have this lines:
    >
    >char *chr;
    >int integ;
    >num[0] = '1';
    >num[1] = '9';
    >
    >How I could obtain value 19 in the integ variable????
    >Hi to all.
    Continue with:

    num[2] = 0;
    sscanf(num,"%d" ,&integ):

    Comment

    • Eric Sosman

      #3
      Re: Convert two char into one int

      Mariano wrote:
      I have this lines:
      >
      char *chr;
      int integ;
      num[0] = '1';
      num[1] = '9';
      >
      How I could obtain value 19 in the integ variable????
      Assuming `num' is an array of characters (two or more),

      integ = (num[0] - '0') * 10 + (num[1] - '0');

      will do it.

      --
      Eric.Sosman@sun .com

      Comment

      • Fred Yu

        #4
        Re: Convert two char into one int


        "Mariano" <mariano.caland ra@gmail.com>
        :8a82a41a-1992-4fa3-9c9b-bc4f6c2b3d6b@e3 9g2000hsf.googl egroups.com...
        >I have this lines:
        >
        char *chr;
        int integ;
        num[0] = '1';
        num[1] = '9';
        >
        How I could obtain value 19 in the integ variable????
        Hi to all.
        >
        integ = atoi(num[0])*10+atoi(num[1]);


        Comment

        • pete

          #5
          Re: Convert two char into one int

          Fred Yu wrote:
          "Mariano" <mariano.caland ra@gmail.com>
          :8a82a41a-1992-4fa3-9c9b-bc4f6c2b3d6b@e3 9g2000hsf.googl egroups.com...
          >I have this lines:
          >>
          >char *chr;
          >int integ;
          >num[0] = '1';
          >num[1] = '9';
          >>
          >How I could obtain value 19 in the integ variable????
          >Hi to all.
          >>
          >
          integ = atoi(num[0])*10+atoi(num[1]);
          That's wrong.
          num[0] is not a pointer to a string.
          atoi takes a pointer to a string as an argument.

          int atoi(const char *nptr);

          --
          pete

          Comment

          • Nick Keighley

            #6
            Re: Convert two char into one int

            On 26 Jun, 02:15, pete <pfil...@mindsp ring.comwrote:
            Fred Yu wrote:
            "Mariano" <mariano.calan. ..@gmail.com>
            :8a82a41a-1992-4fa3-9c9b-bc4f6c2b3...@e3 9g2000hsf.googl egroups.com...
            I have this lines:
            >
            char *chr;
            int integ;
            num[0] = '1';
            num[1] = '9';
            >
            How I could obtain value 19 in the integ variable????
            Hi to all.
            >
            integ = atoi(num[0])*10+atoi(num[1]);
            >
            That's wrong.
            num[0] is not a pointer to a string.
            and num may not be a valid string, we don't
            know if there's a terminating nul character
            atoi takes a pointer to a string as an argument.
            and has poor error handling
            int atoi(const char *nptr);

            --
            Nick Keighley


            Comment

            Working...