a large hex counter

This is a discussion on a large hex counter within the C Programming forums, part of the General Programming Boards category; I need to make a counter using hex I want an array of length 10 to be updated each cycle ...

  1. #1
    Peaches
    Guest

    a large hex counter

    I need to make a counter using hex
    I want an array of length 10 to be updated each cycle with the next hex number.
    So the array would start with:
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1
    ...
    0 0 0 0 0 0 0 0 0 15
    0 0 0 0 0 0 0 0 1 0
    ...
    15 15 15 15 15 15 15 15 15 15

    I previously handled any counter in a different base by having a huge decimal counter then converting it to the appropriate base.
    Unfortunately a long is only 4 bytes and can't handle 16^10
    I can't use doubles with my previous method either because the mod (%) operator doesn't work with them.

    Not sure if I explained the problem well enough (tell me so with a reply)
    Any ideas?

  2. #2
    Peaches
    Guest

    Wait a moment...

    After much thought I've decided its just easier to splt the array into 2 hence halving the max decimal number
    No redesigning for me =)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You could always go with brute force.
    Code:
    #include <stdio.h>
    
    #define SIZE 10
    
    static void clear(unsigned *counter, int from)
    {
        int i;
    
        for (i = from; i < SIZE; i++)
            counter[i] = 0x0;
    }
    
    static void print(unsigned *counter)
    {
        int i;
    
        for (i = 0; i < SIZE; i++)
            printf("%x|", counter[i]);
    
        fflush(stdout);
        printf("\r");
    }
    
    int main(void)
    {
        unsigned counter[SIZE] = {0};
        int i;
    
        while (1)
        {
            counter[SIZE - 1]++;
    
            for (i = SIZE - 1; i > 0; i--)
            {
                if (counter[i] == 0x10)
                {
                    counter[i - 1]++;
                    clear(counter, i);
                }
            }
    
            if (counter[0] == 0x10)
                break;
    
            print(counter);
        }
    
        printf("\n");
    
        return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  4. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  5. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21