Format question

  • Thread starter Thread starter george r smith
  • Start date Start date
G

george r smith

How can you format the following so that all the numbers line up (right
justification) ?
thanks

static void Main(string[] args)
{
for (int sq = 0; sq < 226; sq+=16)
{
int i = 0;
i = sq & 136;
Console.WriteLine(sq + " " + Convert.ToString(sq,2) + " " +
Convert.ToString(sq,16) + " Test = " + i + " " +
Convert.ToString(136,2));
}
}

current printout:
0 0 0 Test = 0 10001000
16 10000 10 Test = 0 10001000
32 100000 20 Test = 0 10001000
48 110000 30 Test = 0 10001000
64 1000000 40 Test = 0 10001000
....

would like
0 00000000 0 Test = 0 10001000
16 00010000 10 Test = 0 10001000
32 00100000 20 Test = 0 10001000
48 00110000 30 Test = 0 10001000
64 01000000 40 Test = 0 10001000
224 11100000 e0 Test = 128 10001000
 
george r smith said:
How can you format the following so that all the numbers line up (right
justification) ?
thanks

static void Main(string[] args)
{
for (int sq = 0; sq < 226; sq+=16)
{
int i = 0;
i = sq & 136;
Console.WriteLine(sq + " " + Convert.ToString(sq,2) + " " +
Convert.ToString(sq,16) + " Test = " + i + " " +
Convert.ToString(136,2));
}
}

current printout:
0 0 0 Test = 0 10001000
16 10000 10 Test = 0 10001000
32 100000 20 Test = 0 10001000
48 110000 30 Test = 0 10001000
64 1000000 40 Test = 0 10001000
...

would like
0 00000000 0 Test = 0 10001000
16 00010000 10 Test = 0 10001000
32 00100000 20 Test = 0 10001000
48 00110000 30 Test = 0 10001000
64 01000000 40 Test = 0 10001000
224 11100000 e0 Test = 128 10001000

Hi George,

Try this:

for (int sq = 0; sq < 226; sq+=16)
{
int i = 0;
i = sq & 136;

Console.WriteLine("{0,3} {1,8} {2,2} Test = {3,-3} {4,8}",
sq,
Convert.ToString(sq,2).PadLeft(8, '0'),
Convert.ToString(sq,16),
i,
Convert.ToString(136,2));
}


Joe
 
Back
Top