help needed on coversin of an char array to an integer

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

    help needed on coversin of an char array to an integer

    Hi,
    Im kinda stuck in a project at a point where i need an array to
    be converted to a
    integer using some kind of math.
    This board does not support functions like scanf, sscanf etc as
    it does not have enough memory to hold their stack.

    given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
    'c' , 'a' };
    i need a function which can convert this array to an integer
    ( 0x0001fbca )

    Expecting a quick response.

    Thanks
    MAx
  • MAx

    #2
    Re: help needed on coversin of an char array to an integer

    On Feb 21, 2:21 pm, MAx <mahesh1...@gma il.comwrote:
    Hi,
          Im kinda stuck in a project at a point where i need an array to
    be converted to a
    integer using some kind of math.
          This board does not support functions like scanf, sscanf etc as
    it does not have enough memory to hold their stack.
    >
          given a string  char str[8] = {'0', '0', '0' , '1' , 'f' ,'b' ,
    'c' , 'a' };
          i need a function which can convert this array to an integer
    ( 0x0001fbca )
    >
     Expecting a quick response.
    >
    Thanks
    MAx
    I forgot to mention that the contents of the array will be in the
    range 0 to 9 and a to f,
    its a hex number read inone digit at a time :-)

    Comment

    • Nick Keighley

      #3
      Re: help needed on coversin of an char array to an integer

      On 21 Feb, 09:21, MAx <mahesh1...@gma il.comwrote:
      Hi,
            Im kinda stuck in a project at a point where i need an array to
      be converted to a
      integer using some kind of math.
            This board does not support functions like scanf, sscanf etc as
      it does not have enough memory to hold their stack.
      >
            given a string  char str[8] = {'0', '0', '0' , '1' , 'f' ,'b' ,
      'c' , 'a' };
            i need a function which can convert this array to an integer
      ( 0x0001fbca )
      >
       Expecting a quick response.
      really?

      Comment

      • vippstar@gmail.com

        #4
        Re: help needed on coversin of an char array to an integer

        On Feb 21, 11:21 am, MAx <mahesh1...@gma il.comwrote:
        Hi,
        Im kinda stuck in a project at a point where i need an array to
        be converted to a
        integer using some kind of math.
        This board does not support functions like scanf, sscanf etc as
        it does not have enough memory to hold their stack.
        Which "board" is that?
        given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
        'c' , 'a' };
        That is not a string as it is not terminated with a 0.
        i need a function which can convert this array to an integer
        ( 0x0001fbca )
        Trivial
        Expecting a quick response.
        Is it you who expects an answer, or your teacher?

        Comment

        • MisterE

          #5
          Re: help needed on coversin of an char array to an integer


          "MAx" <mahesh1280@gma il.comwrote in message
          news:6f4d18ab-ae46-4064-9d51-7251a738f646@n7 7g2000hse.googl egroups.com...
          Hi,
          Im kinda stuck in a project at a point where i need an array to
          be converted to a
          integer using some kind of math.
          This board does not support functions like scanf, sscanf etc as
          it does not have enough memory to hold their stack.
          >
          given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
          'c' , 'a' };
          i need a function which can convert this array to an integer
          ( 0x0001fbca )
          >
          Expecting a quick response.
          >
          Thanks
          MAx
          int i;
          int r = 0;
          for (i=0;i<8;i++)
          {
          if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
          else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
          else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' + 10;
          if (i != 7) r <<=4;
          }


          Comment

          • vippstar@gmail.com

            #6
            Re: help needed on coversin of an char array to an integer

            On Feb 21, 11:34 am, "MisterE" <Mist...@nimga. comwrote:
            "MAx" <mahesh1...@gma il.comwrote in message
            >
            news:6f4d18ab-ae46-4064-9d51-7251a738f646@n7 7g2000hse.googl egroups.com...
            >
            >
            >
            Hi,
            Im kinda stuck in a project at a point where i need an array to
            be converted to a
            integer using some kind of math.
            This board does not support functions like scanf, sscanf etc as
            it does not have enough memory to hold their stack.
            >
            given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
            'c' , 'a' };
            i need a function which can convert this array to an integer
            ( 0x0001fbca )
            >
            Expecting a quick response.
            >
            Thanks
            MAx
            >
            int i;
            int r = 0;
            for (i=0;i<8;i++)
            {
            if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
            else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
            else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' + 10;
            if (i != 7) r <<=4;
            >
            }
            What makes you think 'A'..'F' (and lowercase) are indeed sequential?
            If you plan on posting C code for the OP, then.. at least post code
            that works.

            Given the restrictions of this, here's a solution I suggest:
            --
            #include <stdio.h>

            int main(void) {

            char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' , 'c' , 'a' };
            unsigned int i;
            FILE * foo;

            foo = tmpfile();
            if(foo != NULL) {
            fwrite(str, 1, sizeof str, foo);
            rewind(foo);
            fscanf(foo, "%x", &i);
            fclose(foo);
            }

            return 0;
            }


            Comment

            • Joachim Schmitz

              #7
              Re: help needed on coversin of an char array to an integer

              vippstar@gmail. com wrote:
              On Feb 21, 11:34 am, "MisterE" <Mist...@nimga. comwrote:
              >"MAx" <mahesh1...@gma il.comwrote in message
              >>
              >news:6f4d18a b-ae46-4064-9d51-7251a738f646@n7 7g2000hse.googl egroups.com...
              >>
              >>
              >>
              >>Hi,
              >> Im kinda stuck in a project at a point where i need an array to
              >>be converted to a
              >>integer using some kind of math.
              >> This board does not support functions like scanf, sscanf etc as
              >>it does not have enough memory to hold their stack.
              >>
              >> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b'
              >>, 'c' , 'a' };
              >> i need a function which can convert this array to an integer
              >>( 0x0001fbca )
              >>
              >>Expecting a quick response.
              >>
              >>Thanks
              >>MAx
              >>
              >int i;
              >int r = 0;
              >for (i=0;i<8;i++)
              >{
              > if ((str[i] >= '0')&&(str[i]<='9')) r |= str[i] - '0';
              > else if ((str[i] >= 'A')&&(str[i]<='F')) r |= str[i] - 'A' + 10;
              > else if ((str[i] >= 'a') && (str[i]<='f')) r |= str[i] - 'a' +
              > 10; if (i != 7) r <<=4;
              >>
              >}
              What makes you think 'A'..'F' (and lowercase) are indeed sequential?
              If you plan on posting C code for the OP, then.. at least post code
              that works.
              >
              Given the restrictions of this, here's a solution I suggest:
              Oops, your "-- " makes the rest a signature and snips your code...
              Restored manually:
              >#include <stdio.h>
              >
              >int main(void) {
              >
              char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' , 'c' , 'a' };
              unsigned int i;
              FILE * foo;
              >
              foo = tmpfile();
              if(foo != NULL) {
              fwrite(str, 1, sizeof str, foo);
              rewind(foo);
              fscanf(foo, "%x", &i);
              fclose(foo);
              }
              >
              return 0;
              >}
              the OP claimed not to be able to use scanf and sscanf as his board doesn't
              have this, so what makes you thing he can created files and/or use fscanf?

              Bye, Jojo


              Comment

              • Mark Bluemel

                #8
                Re: help needed on coversin of an char array to an integer

                MAx wrote:
                Hi,
                Im kinda stuck in a project at a point where i need an array to
                be converted to a
                integer using some kind of math.
                This board does not support functions like scanf, sscanf etc as
                it does not have enough memory to hold their stack.
                >
                given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
                'c' , 'a' };
                i need a function which can convert this array to an integer
                ( 0x0001fbca )
                Then write one. It's not exactly difficult.

                * Start with a running total, set initially to 0.
                * Get a digit at a time, convert it into a numeric value,
                multiply the running total by 16 and add the digit's value.
                * When you run out of digits, you're done

                Converting a character (e.g. '9' or 'a') to it's numeric value is a
                little more tricky, but not very.

                According to the standard, '0' - '9' are contiguous in the character
                set, therefore the can always be converted to integers by subtracting
                '0', so that's easy.

                If you can guarantee that your character set has contiguous alphabetics
                (ASCII does, but some representations don't), you can covert alphabetic
                characters to their value in the hexadecimal range by subtracting 'a'
                (or 'A' as appropriate) and adding 10.

                If your character set isn't contiguous (or you're not sure that it will
                be so everywhere that your code may need to run), you'll need to use a
                lookup table or switch block.
                Expecting a quick response.
                Nice to see an optimist.

                Comment

                • Old Wolf

                  #9
                  Re: help needed on coversin of an char array to an integer

                  On Feb 21, 10:43 pm, vipps...@gmail. com wrote:
                  >
                  What makes you think 'A'..'F' (and lowercase) are indeed sequential?
                  Because they were/are sequential on every single
                  system that ever had a C compiler?

                  Comment

                  • Flash Gordon

                    #10
                    Re: help needed on coversin of an char array to an integer

                    Old Wolf wrote, On 22/02/08 12:55:
                    On Feb 21, 10:43 pm, vipps...@gmail. com wrote:
                    >What makes you think 'A'..'F' (and lowercase) are indeed sequential?
                    >
                    Because they were/are sequential on every single
                    system that ever had a C compiler?
                    Are you claiming there were no C compilers for any of the hardware that
                    uses EBCDIC?

                    --
                    Flash Gordon

                    Comment

                    • vippstar@gmail.com

                      #11
                      Re: help needed on coversin of an char array to an integer

                      On Feb 22, 3:47 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                      Old Wolf wrote, On 22/02/08 12:55:
                      >
                      On Feb 21, 10:43 pm, vipps...@gmail. com wrote:
                      What makes you think 'A'..'F' (and lowercase) are indeed sequential?
                      >
                      Because they were/are sequential on every single
                      system that ever had a C compiler?
                      >
                      Are you claiming there were no C compilers for any of the hardware that
                      uses EBCDIC?
                      In EBCDIC, 'a'..'f' and 'A'..'F' are sequential.
                      Even if there is no charset, the standard does not guarantee it, and I
                      believe that's all you need to know.

                      Comment

                      • Richard Tobin

                        #12
                        Re: help needed on coversin of an char array to an integer

                        In article <ed0395xaos.ln2 @news.flash-gordon.me.uk>,
                        Flash Gordon <spam@flash-gordon.me.ukwro te:
                        >>What makes you think 'A'..'F' (and lowercase) are indeed sequential?
                        >Because they were/are sequential on every single
                        >system that ever had a C compiler?
                        >Are you claiming there were no C compilers for any of the hardware that
                        >uses EBCDIC?
                        The gaps in EBCDIC are between I and J, and R and S.

                        -- Richard
                        --
                        :wq

                        Comment

                        • Bartc

                          #13
                          Re: help needed on coversin of an char array to an integer


                          "Richard Tobin" <richard@cogsci .ed.ac.ukwrote in message
                          news:fpmor7$25d a$2@pc-news.cogsci.ed. ac.uk...
                          In article <ed0395xaos.ln2 @news.flash-gordon.me.uk>,
                          Flash Gordon <spam@flash-gordon.me.ukwro te:
                          >
                          >>>What makes you think 'A'..'F' (and lowercase) are indeed sequential?
                          >
                          >>Because they were/are sequential on every single
                          >>system that ever had a C compiler?
                          >
                          >>Are you claiming there were no C compilers for any of the hardware that
                          >>uses EBCDIC?
                          >
                          The gaps in EBCDIC are between I and J, and R and S.
                          Why would whoever put together EBCDIC encoding, put big holes in it like
                          that?

                          It would be more understandable if it was accented versions of some
                          characters, but apparently it's a couple of punctuation characters.

                          Whoever created it should have been fired.

                          Even a child would at least have come up with 1 to 26.

                          --
                          Bart


                          Comment

                          • CBFalconer

                            #14
                            Re: help needed on coversin of an char array to an integer

                            Old Wolf wrote:
                            vipps...@gmail. com wrote:
                            >
                            >What makes you think 'A'..'F' (and lowercase) are indeed sequential?
                            >
                            Because they were/are sequential on every single
                            system that ever had a C compiler?
                            Oh my. You have led a sheltered life. Read the standard.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.



                            --
                            Posted via a free Usenet account from http://www.teranews.com

                            Comment

                            • CBFalconer

                              #15
                              Re: help needed on coversin of an char array to an integer

                              Ben Bacarisse wrote:
                              >
                              .... snip ...
                              >
                              BTW, I think there *should* be an 'int toxvalue(int c);' in
                              standard C since the implementation can do a better job of this
                              simple conversion than any portable code.
                              There is:

                              int hexvalue(int c) {
                              static const char s = "abcdef";
                              return (strchr(s, c)) - &s + 10;
                              }

                              Convert it into a macro if you wish. It returns 16 for a bad c.

                              --
                              [mail]: Chuck F (cbfalconer at maineline dot net)
                              [page]: <http://cbfalconer.home .att.net>
                              Try the download section.



                              --
                              Posted via a free Usenet account from http://www.teranews.com

                              Comment

                              Working...