Ann J
Ann J

Reputation: 25

Binary Conversion method in c#

I am a beginner in programming. Curently am practising C#.How can I get the binary value of a previous number for the given number.

I have already tried using normal binary conversion

for example i want get 000 as binary for value integer 1

Upvotes: 0

Views: 109

Answers (1)

Ephemeral
Ephemeral

Reputation: 423

Maybe:

int binary_base = 2;
int hexadecimal_base = 16;
for (int i = 0; i < 255; i++)
{
     if(i == 0) { continue;  }
     Console.WriteLine(i + " " + Convert.ToString((i - 1), binary_base).PadLeft(8, '0') + " 0x" + Convert.ToString((i - 1), hexadecimal_base).PadLeft(2, '0'));

}

out (zero ignored)

1 00000000 0x00
2 00000001 0x01
3 00000010 0x02
4 00000011 0x03
5 00000100 0x04
6 00000101 0x05
7 00000110 0x06
8 00000111 0x07
9 00001000 0x08
10 00001001 0x09
...
254 11111101 0xfd

Upvotes: 1

Related Questions