-1

I am using this answer to this question in order to view the 0's in my binary value Byte to Binary String C# - Display all 8 digits

int a = 00010

and am converting it into a string with:

string b = Convert.ToString(a).PadLeft(5, '0');

This works perfectly and I can print 00010 instead of 10. However, I need to convert this back to an integer in order to populate an integer array. I am using this to convert it back into a integer:

int c = Convert.ToInt32(b);
Console.WriteLine(c);

Howwever, when I print this it the preceding 0's are missing, and I am printing '10'. Is there any way to convert this back to an integer and keep the preceding 0's? Thanks.

Community
  • 1
  • 1
  • 6
    There is no integer called `00010`. It is equal to `10`. `00010` is just a string representation of it. You can't have an integer as `00010`. – Soner Gönül Apr 06 '14 at 13:31
  • Can you clarify your question a bit, please? It sounds like you are assuming a difference between the integer value *10* and the integer value *00010* (?) – O. R. Mapper Apr 06 '14 at 13:34
  • Is there a reason you can't store it as an integer (10) and convert to a string with preceding 0's for presentation? – Brad Faircloth Apr 06 '14 at 13:34
  • 1
    Also, you're saying it's binary. Does that mean that your `0010` actually represents the number 2? If that's the case, I think storing it in `int` as 10 is the wrong way to do this. – svick Apr 06 '14 at 13:46
  • Maybe it should also be pointed out that already in the question you are linking to, `MyVeryOwnByte` (which conveniently appears without a data type) is *not* actually "a byte", but rather an `int`, and the number assigned are decimal numbers (that happen to consist only of zeroes and ones), even though the author of said question appears to interpret them as bits of a byte. – O. R. Mapper Apr 06 '14 at 14:07

3 Answers3

3

An integer doesn't have leading zeros. Integers are numeric values, they don't carry display information with them. So if you're storing the value as an integer then you're not storing any display information. In that case you apply the display information when you display the value:

int a = 10;
Console.WriteLine(Convert.ToString(a).PadLeft(5, '0'));

If you want the display information to be retained as part of the value itself, make it a string:

string a = "00010";
Console.WriteLine(a);

If it needs to be stored as an integer and you don't want to re-write the display logic many times, you can encapsulate that logic into a custom type which wraps the integer:

public class PaddedInteger
{
    private int Value { get; set; }
    private int PaddingSize { get; set; }
    private char PaddingCharacter { get; set; }

    public PaddedInteger(int value, int paddingSize, char paddingCharacter)
    {
        Value = value;
        PaddingSize = paddingSize;
        PaddingCharacter = paddingCharacter;
    }

    public override string ToString()
    {
        return Convert.ToString(Value).PadLeft(PaddingSize, PaddingCharacter);
    }
}

Then in your code:

PaddedInteger a = new PaddedInteger(10, 5, '0');
Console.WriteLine(a);
David
  • 208,112
  • 36
  • 198
  • 279
2

I feel like taking a risk to answer but anyway..

An integer is a mathematical representation of a number and is ignorant of leading zeroes. There is no integer called 00010. It is already equal to 10.

00010 is just a string representation of it. You can't have an integer as 00010 at all.

Even when you define it in your code, result will be without zeros.

int i = 00010;
Console.WriteLine(i); // Prints 10
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Maybe the last sentence should be clarified in that the result will be agnostic of any *digits* whatsoever; it will just be the numerical value of *10*, or in other words, the numerical value of the result of *2 times 5*. (Of course it's stored in a binary way internally, but that shouldn't matter; the code should still work even when compiled for a hypothetical computer that does *not* internally work with bits.) – O. R. Mapper Apr 06 '14 at 13:46
  • 1
    @Downvoter care to comment at least so I can see where I might be wrong? – Soner Gönül Apr 07 '14 at 14:09
0

Consult the documentation for formatting integers to strings via the int.ToString(string, IFormatProvider) method overload.

http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx

Martin Costello
  • 9,672
  • 5
  • 60
  • 72