deucalion0
deucalion0

Reputation: 2440

How to stop leading 0's from being stripped from my integers in C#

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);
         }
     }
}

Upvotes: 1

Views: 2252

Answers (5)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98810

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"));

Upvotes: -1

Damith
Damith

Reputation: 63095

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.

Upvotes: 5

CodeCaster
CodeCaster

Reputation: 151664

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.

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300719

You are confusing presentation with representation, try:

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

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460208

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.

Upvotes: 7

Related Questions