tags:

views:

15

answers:

1

I want to convert numbers from 0 to 15 like that

0000
0001
0010
0011
.
.
.
1111

Problem is that when we convert 2 to binary number it gives only 10 in binary, but i want to convert 2 to 4 bit binary number 0010.

+1  A: 

This code should do what you're looking for:

For i As Integer = 0 To 15
    Console.WriteLine(Convert.ToString(i, 2).PadLeft(4, "0"C))
Next

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

The "2" in Convert.ToString(i, 2) means binary. PadLeft(4, "0"C) means that if the string isn't four characters, append zeros to the beginning until it is four characters.

Merlyn Morgan-Graham
Thanks man u saved my day. Here is vb version.
m.qayyum
For i As Integer = 0 To 15 Console.WriteLine(Convert.ToString(i, 2).PadLeft(4, "0"c))Next i
m.qayyum
@m.qayyum: Thanks. I found an online converter. Glad it gave the right output ;)
Merlyn Morgan-Graham
Ya i also used c to vb converter ;)
m.qayyum
http://tangiblesoftwaresolutions.com/
m.qayyum