0

Possible Duplicate:
How to pad a binary string with zeros?

I am able to convert decimal numbers to binary but not in an 8 bit format. For example if I input 5 the result is 101 which is correct but how can I show the result in an 8 bit format such as 00000101?

string result = Convert.ToString(num, 2).PadLeft(8, '0');
Community
  • 1
  • 1
  • Despite the edit, the question remains an exact duplicate. I see no reason to reopen – rds Jan 20 '13 at 23:02

1 Answers1

11

The simplest way would be to use

string result = Convert.ToString(num, 2).PadLeft(8, '0');
benjer3
  • 657
  • 6
  • 13