C# – How to count in another base

binaryc

Say I want to count in binary until I have the highest number a set number of bytes will hold, how could I do that? It feels like there should be a pretty simple way, I just don't know it. I googled but was surprised to not find any examples.

For example, if I wanted to count to 1 byte I'd start with 00000001 add 1 and get 00000010, add 1 to get 00000011, etc until I get to 11111111.

Additionally, how could you do the same in hex? you start with 0x00, add one and output 0x01, the 0x02, 0x03, etc until you get to 0xFF?

Also, how can I output the values as a string (like my examples)?

Some psuedo-code:

byteSize = 3
counter = 0
    while counter.size <= byteSize
      print counter /* prints 00000001, 00000010, etc. 
      count += 1
    loop

Update:

I'm not only concerned with displaying a number in another base, that was only part of it. I see my error in that the displaying function is what determines how the number is displayed (as Jeremy pointed out). So, that's parts not a problem.

Update 2:

I promise I'm not a complete moron. Here is the context:

It started today when I read this on reddit:
http://www.elliottkember.com/kember_identity.html

Then, this:
http://www.reddit.com/r/programming/comments/8iguu/md5_gamechallenge_for_you_reddit/

which led to this:
http://www.olegkikin.com/md5game/

So, I figured you could just count in bits starting at different intervals and just let 'er run.

Best Answer

For arbitrary depth.

You could switch this to 64-bit easily if that makes sense to do so (e.g., 64-bit processor).

Note: this is entirely free-hand and I have not compiled it nor, obviously, tested it. I can't even begin to guess how long it would take to print out 2^160 values (that's 1.46e48 values) or more if you do more than 5 32-bit counters.

This is grossly inefficient but what the heck.

// A word is 32-bits
void CountBytes(int numberOfWords)
{
    uint[] numbers = new uint[numberOfWords];

    while(true)
    {
        // Show most-significant first
        for (int i=numbers.Length-1; i>=0; i--)
        {
            Console.Write(Convert.ToString(numbers[i], 2).PadLeft(32, '0'));
        }

        // Hit max on all uint's, bail
        bool done = true;
        for (int i=numbers.Length-1; i >= 0; i--)
        {
            if (numbers[i] != uint.MaxValue)
            {
                done = false;
                break;
            }
        }
        if (done)
        {
            break;
        }

        // Check for overflow
        for (int i=numbers.Length-2; i >= 0; i--)
        {
            // Overflow for numbers[i] is if it and all beneath it are MaxValue
            bool overflow = true;
            for (int k=i; k>=0; k--)
            {
                if (numbers[k] != uint.MaxValue)
                {
                    overflow = false;
                    break;
                }
            }

            if (overflow)
            {
                numbers[i+1]++;
                numbers[i] = 0;
            }
        }

        // Increment counter
        numbers[0]++;
    }
}