Thread: convert char array to integer number

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Thanks Adak, that was indeed what I was trying to do.

    Yes, I really should have removed that comment..

    Would it be possible to ask why you set up the for loop like that and what tmp2 does, please?

    --dave

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by droseman View Post
    Thanks Adak, that was indeed what I was trying to do.

    Yes, I really should have removed that comment..

    Would it be possible to ask why you set up the for loop like that and what tmp2 does, please?

    --dave
    Sure! Go ahead and ask!


    Just messing with ya!

    That's just the standard way to walk through an array, decrimenting. Tmp2 is just a placeholder. We need that value before we can start the next line of code. I suppose you could do something with parentheses, but the simpler your code logic is, (generally), the better.

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Cheers for that.

    Is there any way this can get itself stuck in an infinite loop, since that is what has happened when I put it into my main (embedded) application.

    --dave

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you note that I changed the name of the array in my example?

    (I dislike long array names).

    Do you need to keep this char array as just char's and not add the end of string char to the end of it? Doing so would elevate it to a string, and then the standard string to number functions could be used.

    Post up your code that shows the endless loop, and I'll see what's up with it.

  5. #5
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Hi Adak,

    I have solved it myself, here is the code which worked (I did remember to change the name of the array - I have a few arrays in my code (as you have probably realised, this is a small module) and it helps me to see which one is which.

    Code:
    for(lc_count = 0;lc_count < 5; lc_count++) 
        {
            hour_tmp = hours_array[i] - '0';
            hour_tmp *= array_mult;
            hourCount = hourCount + hour_tmp;
            array_mult *= 10;
            --i;
        }
    My compiler didn't like using i as a loop control parameter (the compiler is an ARM Cortex M3 compiler from Raisonance), it just never exited the loop and made hourCount invalid when i went undefined.

    Thanks again for all your help

    --dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM