tags:

views:

263

answers:

4

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.

+5  A: 

Binary

for (int i = 0; i <= byte.MaxValue; i++)
{
    Console.WriteLine(Convert.ToString(i, 2).PadLeft(8, '0'));
}

Hexadecimal

for (int i = 0; i <= byte.MaxValue; i++)
{
    Console.WriteLine("0x" + i.ToString("X").PadLeft(2, '0'));
}

or

for (int i = 0; i <= byte.MaxValue; i++)
{
    Console.WriteLine(Convert.ToString(i, 16).PadLeft(2, '0'));
}

Multiple bytes

int numBytes = 3;
for (int i = 0; i < Math.Pow(2, numBytes * 8); i++)
{
    Console.WriteLine(Convert.ToString(i, 2).PadLeft(numBytes * 8, '0'));
}

I wouldn't do more than 3, or you're going to be waiting a very long time...

Response to Update:

I hope you were kidding about "counting to 20 bytes in binary". That's 160 bits. That creates a list of numbers with a count somewhere in the realm of the number of atoms in the entire universe. I hope you have plenty of time (i.e. googols of millenia) to wait for that to finish :)

John Rasch
OK, so that's to 1 byte, what about to 2 or 3 bytes.
scottm
Uhh, increase the upper bound of the loop and pad left more accordingly?
lc
It seems this only works for certain bases (2, 8, 10, 16). If the OP wanted something more general... let x be an integer and b be the base. x%b is the lowest digit in your representation and x /= b will shift down. etc...
Derek E
Yeah, but when i gets to 2147483647 you roll over to -2147483648, not 2147483648. So, you could use UInt64, but then how would you handle it after UInt64.MaxValue?
scottm
It's really pointless to go that far because it would take a lifetime to even get there, but you could just use recursion and concatenate the first x bytes with the last 4.
John Rasch
ha, the eventual plan was to get to 32!
scottm
+5  A: 

The base that you're "counting" in is all up to the function that outputs the number to the screen (or converts it to a string before doing so). Different bases are just different ways of visually representing the same numbers. So to "count" in binary, you would simply tell your number-to-string function to use base 2 instead of the default base 10.

yjerem
I know they are the same numbers, and it makes sense that the displaying function is what determines how it's displayed (doh). If my counting variable is an unsigned int, then the max it can hold is 4 bytes of binary data (11111111111111111111111111111111) or 4294967295. So, how do I continue to count in binary to say 20 bytes?
scottm
What you just said is a completely different question, and you should have stated so originally. To continue to count, just think about how you count normally: if you run out of room, you go to the next column. For example, if you want to count, you can just use nested loops, one for the high word and another for the low.
lc
No, I specified "a set number of bytes", my example was just to 1 byte so I didn't fill the page with 1s.
scottm
+1  A: 

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]++;
    }
}
Colin Burnett
This is the kind of approach I was looking for, but I bet there's a way to implement it so you don't have to add more conditionals when you add more bytes.
scottm
I made the checks so it can be arbitrary number of words. Still untested and uncompiled.
Colin Burnett
A: 

28 * bytes - 1

Example for 1 byte:

28 * 1 byte
= 28
= 255

That's the highest number (integer) you can store in 1 byte.

Fowl