help needed on coversin of an char array to an integer

Discussion in 'C Programming' started by MAx, Feb 21, 2008.

  1. MAx

    MAx Guest

    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, Feb 21, 2008
    #1
    1. Advertising

  2. MAx

    MAx Guest

    On Feb 21, 2:21 pm, 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 )
    >
    >  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 :)
     
    MAx, Feb 21, 2008
    #2
    1. Advertising

  3. On 21 Feb, 09:21, 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 )
    >
    >  Expecting a quick response.


    really?
     
    Nick Keighley, Feb 21, 2008
    #3
  4. MAx

    Guest

    On Feb 21, 11:21 am, 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.

    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?
     
    , Feb 21, 2008
    #4
  5. MAx

    MisterE Guest

    "MAx" <> wrote in message
    news:...
    > 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 >= '0')&&(str<='9')) r |= str - '0';
    else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
    else if ((str >= 'a') && (str<='f')) r |= str - 'a' + 10;
    if (i != 7) r <<=4;
    }
     
    MisterE, Feb 21, 2008
    #5
  6. MAx

    Guest

    On Feb 21, 11:34 am, "MisterE" <> wrote:
    > "MAx" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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 >= '0')&&(str<='9')) r |= str - '0';
    > else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
    > else if ((str >= 'a') && (str<='f')) r |= str - '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;
    }
     
    , Feb 21, 2008
    #6
  7. wrote:
    > On Feb 21, 11:34 am, "MisterE" <> wrote:
    >> "MAx" <> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >>> 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 >= '0')&&(str<='9')) r |= str - '0';
    >> else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
    >> else if ((str >= 'a') && (str<='f')) r |= str - '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
     
    Joachim Schmitz, Feb 21, 2008
    #7
  8. MAx

    Guest

    On Feb 21, 12:00 pm, "Joachim Schmitz" <nospam.j...@schmitz-
    digital.de> wrote:
    > wrote:
    > > On Feb 21, 11:34 am, "MisterE" <> wrote:
    > >> "MAx" <> wrote in message

    >
    > >>news:...

    >
    > >>> 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 >= '0')&&(str<='9')) r |= str - '0';
    > >> else if ((str >= 'A')&&(str<='F')) r |= str - 'A' + 10;
    > >> else if ((str >= 'a') && (str<='f')) r |= str - '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:

    Ah, that was silly.
    I should've written "-- snip.c --" as I usually do. Oh well.
    > >#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?

    I only wrote a solution given the restrictions. :)
    Ofcourse there is a "better" solution, and I suspect the one that OP
    looks for.
    However, it sounds like a homework assignment because the task is
    quite trivial; furthermore I believe it would "harm" OP and others
    more to give a working solution than to make him work it on his own.
     
    , Feb 21, 2008
    #8
  9. MAx

    Willem Guest

    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 )

    Since you're talking about a 'board' I assume that you have a single,
    fixed platform, and that your character set is ASCII.

    I also assume that char is 8 bits and unsigned long is at least 32 bits,
    that you will never have malformed input, and that you want a lean and
    mean solution.

    This makes the following (quite evil) code possible:

    unsigned long hex2num(unsigned char str[8])
    {
    unsigned long *res, dig;
    res = (unsigned long *)str;
    dig = ((*res & 0x10101010) >> 4) * 0x0f;
    *res = (*res & dig) | ((*res + 0x09090909) & (dig ^ 0x0f0f0f0f));
    res = (unsigned long *)(str + 4);
    dig = ((*res & 0x10101010) >> 4) * 0x0f;
    *res = (*res & dig) | ((*res + 0x09090909) & (dig ^ 0x0f0f0f0f));

    return (str[7] ) + (str[6] << 4)
    + (str[5] << 8 ) + (str[4] << 12)
    + (str[3] << 16) + (str[2] << 20)
    + (str[1] << 24) + (str[0] << 28);
    }

    Which should translate nicely into assembly.
    (The last bit with the return might be sped up a bit, still).


    As an exercise to the reader: How many assumptions does this
    code make that are outside the C standard ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Feb 21, 2008
    #9
  10. MAx

    Mark Bluemel Guest

    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.
     
    Mark Bluemel, Feb 21, 2008
    #10
  11. MAx

    Bartc Guest

    "MAx" <> wrote in message
    news:...
    > 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 )


    I assume sometimes the string will be something else? Otherwise it's rather
    trivial.

    Where are the hex digits coming from? If from a hex keyboard, then each
    digit may already be 0 to 15 then combining to an integer value is very easy
    (each digit corresponds to a 4-bit chunk).

    Complete solutions have already been posted, the only tricky bit is
    converting each character, 0 1 2 3 4 5 6 7 8 9 a b c d e f (and maybe A B C
    D E F?) to a value 0 to 15.

    --
    Bart
     
    Bartc, Feb 21, 2008
    #11
  12. MAx

    MAx Guest

    On Feb 22, 12:55 am, "Bartc" <> wrote:
    > "MAx" <> wrote in message
    >
    > news:...
    >
    > > 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 )

    >
    > I assume sometimes the string will be something else? Otherwise it's rather
    > trivial.
    >
    > Where are the hex digits coming from? If from a hex keyboard, then each
    > digit may already be 0 to 15 then combining to an integer value is very easy
    > (each digit corresponds to a 4-bit chunk).
    >
    > Complete solutions have already been posted, the only tricky bit is
    > converting each character, 0 1 2 3 4 5 6 7 8 9 a b c d e f (and maybe A B C
    > D E F?) to a value 0 to 15.
    >
    > --
    > Bart





    Hey,
    Thanks guys, got the code to work. MisterE's solution did the
    trick. Thanks man.
     
    MAx, Feb 22, 2008
    #12
  13. MAx said:

    <snip>

    > Hey,
    > Thanks guys, got the code to work. MisterE's solution did the
    > trick. Thanks man.



    Unfortunately, there are circumstances in which that code *won't* do the
    trick. Presumably you have investigated these circumstances and determined
    that they will never apply to your code's user base?


    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 22, 2008
    #13
  14. MAx

    MisterE Guest


    > 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.


    I don't bother assuming he is one of 0.0000000000000000001% of people who
    don't use ASCII character set for raw chars in c.
     
    MisterE, Feb 22, 2008
    #14
  15. MisterE said:

    > I don't bother assuming he is one of 0.0000000000000000001% of people who
    > don't use ASCII character set for raw chars in c.


    If we assume everyone on the planet - around 6600000000 people - uses a
    computer, then 0.0000000000000000001% of this would be 0.0000000000066
    people. Effectively, you're claiming that at most one person on the planet
    uses a non-ASCII system.

    You are mistaken.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Feb 22, 2008
    #15
  16. MAx

    Bartc Guest

    "Richard Heathfield" <> wrote in message
    news:...
    > MAx said:
    >
    > <snip>
    >
    >> Hey,
    >> Thanks guys, got the code to work. MisterE's solution did the
    >> trick. Thanks man.

    >
    >
    > Unfortunately, there are circumstances in which that code *won't* do the
    > trick. Presumably you have investigated these circumstances and determined
    > that they will never apply to your code's user base?


    MisterE has already indicated what he thinks the probability of that is:
    something less than one person in the world (was it one trillionth of a
    person?) having such a system. Well I wouldn't go that far myself..

    MAx was talking about a board, which either gets distributed with the code,
    or the customers have the same model.

    But, you might be right, maybe in Europe they like keying in hex numbers
    with accents (0001fbcá). That wouldn't work.

    --
    Bart
     
    Bartc, Feb 22, 2008
    #16
  17. MAx

    Old Wolf Guest

    On Feb 21, 10:43 pm, 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?
     
    Old Wolf, Feb 22, 2008
    #17
  18. MAx

    Willem Guest

    Richard wrote:
    ) Unfortunately, there are circumstances in which that code *won't* do the
    ) trick. Presumably you have investigated these circumstances and determined
    ) that they will never apply to your code's user base?

    For the record, EBCDIC has A..F in sequential order, (and a..f as well).


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Feb 22, 2008
    #18
  19. MAx

    Guest

    On Feb 22, 2:55 pm, Old Wolf <> wrote:
    > On Feb 21, 10:43 pm, 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?

    Let us assume you are correct here, what does that guarantee about the
    future?
    The standard only mentions '0'..'9' to be sequential.
     
    , Feb 22, 2008
    #19
  20. In article <>,
    <> 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?


    >Let us assume you are correct here, what does that guarantee about the
    >future?


    That the chance of your program being used on a computer with a
    character set with non-sequential A-F is less than, say, the chance of
    the computer being eaten by beetles?

    -- Richard
    --
    :wq
     
    Richard Tobin, Feb 22, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
  2. Replies:
    3
    Views:
    758
  3. davidb
    Replies:
    0
    Views:
    809
    davidb
    Sep 1, 2006
  4. davidb
    Replies:
    6
    Views:
    1,596
    Default User
    Sep 1, 2006
  5. Replies:
    5
    Views:
    245
    Tassilo v. Parseval
    Nov 22, 2005
Loading...

Share This Page