converting char array to integer

Discussion in 'C Programming' started by silentlights, Apr 15, 2004.

  1. silentlights

    silentlights Guest

    Hi,
    Can you help me conver a char array into an integer. I am trying something
    like..

    char carray[5];
    int numb;

    carray[0] = 1;
    carray[1] = 5;
    carray[2] = 1;
    carray[3] = 5;

    numb = (int) carray; // this is what I want to convert

    I know this doesnt work. Does anyone of you have a suggestion for that
    conversion. This is to be implemented in a 16bit µc. Is there a solution
    without using anything like sprintf or multiplication.

    Thanks in advance
    Densil
     
    silentlights, Apr 15, 2004
    #1
    1. Advertisements

  2. What would you want 'numb' to be after the conversion?

    1515 ?
    5151 ?
    12 ?

    Something else
     
    Nils Petter Vaskinn, Apr 15, 2004
    #2
    1. Advertisements

  3. #include <stdlib.h>

    char carray[5];
    int numb;
    ....
    carray[0] = '1'; // 1 != '1'
    carray[1] = '5';
    carray[2] = '1';
    carray[3] = '5';
    carray[4] = 0; // null termination of string

    numb = atoi(carray);
    ....
     
    Stephen Sprunk, Apr 15, 2004
    #3
  4. What you could also do (but this will only work on 32-bit micro-processors)
    carray[0] = 1;
    carray[1] = 5;
    carray[2] = 1;
    carray[3] = 5;
    numb = (int *) carray; // carray is a char* already so cast it into an int
    *

    As for the case you're mentioning, if int's are 16 bits and char's are 8
    bits this will only set num to either 15 or 51 depending on the endianity of
    the machine you're using.

    Ahmed
     
    Ahmed S. Badran, Apr 15, 2004
    #4
  5. Besides it being a brain-dead idea in the first place, you could've
    well put any garbage in the array, since you only try to assign the
    pointer value carray decays to in value context (sic) to an integer
    variable, after performing a useless type cast, BTW.

    Regards
     
    Irrwahn Grausewitz, Apr 15, 2004
    #5
  6. Sorry guys, the correction is

    numb = *((int *)carray);

    Ahmed
     
    Ahmed S. Badran, Apr 15, 2004
    #6
  7. silentlights

    silentlights Guest

    Hi Nils,

    I need to have numb = 1515

    by the way..

    char str[5] = "5678";
    int intvar = (int *)str;

    doesnt work either.

    I thank everyone here who is reading this messg and trying to post a
    reply. Your help is highly appreciated.

    Cheers
    Densil
     
    silentlights, Apr 15, 2004
    #7
  8. Densil,

    char str[5] = "5678";

    /* is NOT */

    char str[5];
    str[0] = 5;
    str[1] = 6;
    str[2] = 7;
    str[3] = 8;
    str[4] = 0; /* or null or whatever.. */

    to convert

    char str[5] = "5678"; /* to int */

    use

    numb = atoi(str);

    to covert the earlier case (of str = 5 + i;) you can use

    numb = * ((int *) str); /* in this case numb will be 5678 if you're on a
    big endian system */

    Ahmed

    Ahmed
     
    Ahmed S. Badran, Apr 15, 2004
    #8
  9. silentlights

    silentlights Guest

    Hi Guys,
    I thank everyone who took time to write me an idea.

    (1) I cant use the function "atoi" because I cant use any standard
    libraries as headers.

    (2) It is a 16bit env, neither can I use the *((int *) str) way.

    The only method I can use.. without any multiplication is...

    unsigned int intvar = 0;
    char str[4] = {'4','3','2','1'};
    unsigned int tau[11] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000,
    9000};
    unsigned int hun[11] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};
    unsigned int ten[11] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};

    intvar = tau[str[0] - 48] + hun[str[1] - 48] + ten[str[2] - 48] + str[3] -
    48;


    If anyone of you find there is something fishy.. pls tell me.. if there
    could be any other way.. without using functions like "atoi" or any "str*"
    functions.. wihtout using multiplication.. pls post me here.

    Thank you all once again
    Densil
     
    silentlights, Apr 15, 2004
    #9
  10. I don't understand /why/ you cannot use multiplication. However, you
    can emulate it using shifts and additions. For example, the macro

    #define MUL10(x) (((x) << 3) + ((x) << 1))

    emulates multiplication by 10.

    Martin
     
    Martin Dickopp, Apr 15, 2004
    #10
  11. silentlights

    Jack Klein Guest

    Don't use atoi(), or any of the ato... functions, unless you can
    guarantee the converted result will not be out of range for the return
    type. Undefined behavior results.

    The strto... functions, on the other hand, have defined behavior no
    matter what the input.


    1. No, the result will not be 5678 on any conforming C
    implementation. Not a chance. C requires a pure binary notation for
    the integer types.

    2. In the original post, the OP stated he was using a 16 bit uC. At
    best, accessing an array of four bytes will result in only the first
    two of them being accessed.

    3. str might not meet the alignment requirements for the address of
    an int on many 16-bit platforms. Results can range from accessing the
    wrong bytes of memory to hardware traps. Undefined behavior is like
    that.
     
    Jack Klein, Apr 16, 2004
    #11
  12. silentlights

    silentlights Guest

    Hi Martin,
    it is expensive for the µC.
    #define MUL10(x) (((x) << 3) + ((x) << 1))
    Fast multiplication is a better solution. It saves a lot of opcodes.

    Thanks
    Densil
     
    silentlights, Apr 16, 2004
    #12
  13. silentlights

    CBFalconer Guest

    Pseudo assembly, 10 * R1 -> R2. R2, flags disturbed. Fast.

    mov r2,r1; operands dest,src
    add r2,r2; 2*
    add r2,r2; 4*
    add r2,r1; 5*
    add r2,r2; 10*

    and the equivalent macro:

    #define MUL10(x) (((x) + ((x) << 2)) << 1)
     
    CBFalconer, Apr 16, 2004
    #13
  14. silentlights

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    CBFalconer wrote:
    | Martin Dickopp wrote:
    |
    | ... snip ...
    |
    |>I don't understand /why/ you cannot use multiplication. However,
    |>you can emulate it using shifts and additions. For example, the
    |>macro
    |>
    |> #define MUL10(x) (((x) << 3) + ((x) << 1))
    |>
    |>emulates multiplication by 10.
    [snip]
    |
    | #define MUL10(x) (((x) + ((x) << 2)) << 1)
    |

    Sweet!


    - --

    Lew Pitcher, IT Consultant, Enterprise Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFAf8wqagVFX4UWr64RAiwWAJ0TLwPUSAY6i5RxKmoyQVje+Ey4eACfdTte
    wqIYy3T7DgKJMFw73IM6y7s=
    =LAKV
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Apr 16, 2004
    #14
  15. silentlights

    silentlights Guest

    Could you please explain this shift.. I didnt get you.

    How can I emulate multiplication for 100 and 1000 ?
    Can I do something similair for division of 10, 100 and 1000 too ?..
    Is there an alternative for modulo of 10, 100 and 1000 ?


    Thanks
    Densil
     
    silentlights, Apr 16, 2004
    #15
  16. silentlights

    CBFalconer Guest

    You failed to quote anything, so I have no idea what you are
    talking about. I have better things to do than search through
    discarded newsgroup entries.
     
    CBFalconer, Apr 16, 2004
    #16
  17. silentlights

    kal Guest

    Could you please explain this shift.. I didnt get you.
    Multiplication is, by definition, repeated addition.

    To multiply by 10 add the number to itselt 9 times.

    result = x + x + x + x + x + x + x + x + x + x

    Sincethe shift operations are fast and left shifting by 1 bit is
    the same as multiplying by 2 one can rework this scheme.

    result = (x << 1) + (x << 1) + (x << 1) + (x << 1) + (x << 1)

    On second thoughts,

    result = (x << 2) + (x << 2) + (x << 1)

    Come to think of it,

    result = (x << 3) + (x << 1)

    Note that: 10 = 8 + 2
    100 = 64 + 32 + 4
    1000 = 512 + 256 + 128 + 64 + 32 + 8

    It might be worthwhile to get hold of one of those hand cranked
    adding machines and practice multiplication/division on it.
     
    kal, Apr 17, 2004
    #17
  18. Maybe more useful: 1000 = 1024 - 16 - 8.
     
    Christian Bau, Apr 17, 2004
    #18
  19. silentlights

    Joe Wright Guest

    Any book on C explains the << and >> shift operators. Do you have a
    book? I recommend K&R2. Given two examples of MUL10 in recent posts,
    why can't you imagine MUL100 and MUL1000? Division is another
    animal. I'll have to get back to you on that.
     
    Joe Wright, Apr 17, 2004
    #19
  20. silentlights

    CBFalconer Guest

    I have mounted a slightly revised version of my recent posting on
    the double-dabble algorithm, which provides a divide by 10
    facility for machines without hardware division, as dubldabl.txt
    at:

    <http://cbfalconer.home.att.net/download/>
     
    CBFalconer, Apr 17, 2004
    #20
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.