1

I am trying to store integers representing 4 bit strings, some of which have 0's at the beginning. When I write these values out in the Console the leading 0's are stripped off, can I stop this from happening? I read through the documentation but couldn't see anything that would prevent this from happening. Here is my code so far:

class Fitness
{
    Random random = new Random();

     int[] myArray = new int[15];
     int[] myArray2 = new int[6];
     int[] numbers = new int[6];
     int randomNumber;
     public void setup()
     {

         for (int i = 0; i < 6; i++)
         {
             do
             {
                 randomNumber = random.Next(1, 16);
             }
             while (numbers.Contains(randomNumber));

             numbers[i] = randomNumber;

         }
         Array.Sort(numbers);
         foreach (int i in numbers)
         {
             Console.WriteLine(i);
         }
         Console.WriteLine("-----------------");
         Console.WriteLine("-----------------");

         myArray[0] = 0001;
         myArray[1] = 0010;
         myArray[2] = 0011;
         myArray[3] = 0100;
         myArray[4] = 0101;
         myArray[5] = 0110;
         myArray[6] = 0111;
         myArray[7] = 1000;
         myArray[8] = 1001;
         myArray[9] = 1010;
         myArray[10] = 1011;
         myArray[11] = 1100;
         myArray[12] = 1101;
         myArray[13] = 1110;
         myArray[14] = 1111;

         for (int i = 0; i < 6; i++)
         {
             myArray2[i] = myArray[numbers[i]-1];
         }
         foreach (int i in myArray2)
         {
             Console.WriteLine(i);
         }
     }
}
deucalion0
  • 2,422
  • 9
  • 55
  • 99
  • 5
    you are confusing presentation with representation – Mitch Wheat Apr 17 '13 at 11:43
  • Do you have to use an integer or would a BitArray be better? – thealfreds Apr 17 '13 at 11:44
  • Can you explain what you mean Mitch? My end goal is to run some Genetic Algorithm Fitness tests comparing binary values, I was thinking that maybe there is a Better data type for this purpose. – deucalion0 Apr 17 '13 at 11:48
  • 2
    If the actual printing isn't the problem you could create a bit matrix. I found this: http://www.pvladov.com/2012/05/bit-matrix-in-c-sharp.html through google. – thealfreds Apr 17 '13 at 11:54
  • 1
    Thanks thealfreds, I guess this approach is similar to what I am trying to achieve, I will experiment with a Bitmatrix! – deucalion0 Apr 17 '13 at 11:58
  • Are you sure this `myArray[5] = 0110;` does what you think it does? Based on the other similar lines of code, it would seem that you think this assigns the binary value 6 (0110) to index 5 of the array, when in fact it assign the value 110 (one hundred ten) to that index. – Chris Dunaway Apr 17 '13 at 15:08
  • Chris I found out that that is indeed what it means and not what I wanted it to mean. I went back to the drawing board! :) – deucalion0 Apr 17 '13 at 15:17

5 Answers5

7

Int32 doesn't have leading zeros but a string can have. You need to apply the correct format. You can use the decimal ("D") format specifier in ToString:

 foreach (int i in myArray2)
 {
     Console.WriteLine(i.ToString("d4"));
 }

Standard Numeric Format Strings

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • That makes a lot of sense with regards to the data type int32, so it seems I need to research a way to use binary values in C# if that even exists. Thanks! – deucalion0 Apr 17 '13 at 11:53
5

try with

Console.WriteLine(i.ToString("D4"));

Read more about Standard Numeric Format Strings...

And this is related to this question C# convert int to string with padding zeros?

edit

you can do what ever you want with int array, when you display format the string as above.

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
  • This worked , but now the result is a string which means I cant do maths later on, what exactly is "D4"? – deucalion0 Apr 17 '13 at 11:50
  • I accepted your answer as the best due to it being the first that gave me this solution and also because of the information you provided. Thanks for helping me get to a stage where I can progress with my program. There were a few good answers! – deucalion0 Apr 17 '13 at 12:00
2

You are confusing presentation with representation, try:

Console.WriteLine(i.ToString("0000")); 
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1

You cannot do this. An Integer (int32) is stored in 4 bytes, so if you want to store 1 (or 01 or 00000000001) then the bytes look like this (actually the other way around on x86, but big-endian is easier readable for us humans):

00000000 00000000 00000000 00000001 

Now when printing this value, how would the CPU or your program or whoever is involved know that you want to print only three zeroes and not the thirty-one that are in memory?

You can either use a different data type (string, array, whatever fits your needs best) or fix the amount of zeroes while printing like the other answers suggest.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
-1

As Tim said, string can leading zeros, Int32 can't.

You can use Decimal ("D") Format Specifier

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.

Console.WriteLine(i.ToString("D4"));
Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • You just literally copied @Tims answer. – CodeCaster Apr 17 '13 at 11:57
  • @CodeCaster NO! I just wrote my answer which start with **you can use..** part, and then I realize Tim already said the **string can leading zeros, Int32 can't.** part, I just add it to my answer with also refering to Tim's answer. This is not copying! – Soner Gönül Apr 17 '13 at 11:59
  • Even if you remove the first line of your answer, it still says the same as Tims answer, with the same quote and the same code, four minutes later. It's just redundant. – CodeCaster Apr 17 '13 at 12:06
  • 1
    I thought the same as you since a while ago, I already asked a [question](http://meta.stackexchange.com/q/94004/158761) for this. If you read this, you will probably change your idea about the **redundant** part `;)` – Soner Gönül Apr 17 '13 at 12:12
  • It says _"One thing that I would support is encouraging users to delete their own answers once they realize it's a duplicate"_, which I support too. – CodeCaster Apr 17 '13 at 12:16
  • You can support that part but I don't think that supporting needs downvotes. Anyway, just two different opinions.. – Soner Gönül Apr 17 '13 at 12:24