0

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

Ann J
  • 25
  • 7
  • Int 1 is binary 1, not 000. Possible duplicate of https://stackoverflow.com/questions/1961599/how-to-convert-binary-to-decimal – Lucca Ferri Feb 05 '19 at 04:32
  • Possible duplicate of [Convert integer to binary in C#](https://stackoverflow.com/questions/2954962/convert-integer-to-binary-in-c-sharp) – Lucca Ferri Feb 05 '19 at 04:34
  • maybe with -1 on your Int and then get binary representation of decremented value ? ( 1 - 1 = 0), but what is your goal ? – Ephemeral Feb 05 '19 at 04:36
  • @Aborted-Security If am entering value 1 i need to 000 as binary value etc (for value 2 it should be 001) etc – Ann J Feb 05 '19 at 05:08
  • @LuccaFerri for decimal value 1 i need to get 000 for 2 - 001 etc – Ann J Feb 05 '19 at 05:10
  • value = value - 1; and then get the binary as said on the thread? – Lucca Ferri Feb 05 '19 at 14:55

1 Answers1

1

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
Ephemeral
  • 423
  • 6
  • 13