Convert Byte to Bit Pattern

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone tell me how to convert a byte to bit pattern?

e.g. Byte b = 1; after conversion = 00000001

Tedmond
 
Tedmond said:
Can anyone tell me how to convert a byte to bit pattern?

e.g. Byte b = 1; after conversion = 00000001

Convert.ToString(byte value, int base)

eg

byte b = 1;
Console.WriteLine (Convert.ToString(b, 2));

Note that that won't pad the result with zeroes. You'll need to do
something like:

Convert.ToString(b, 2).PadLeft(8, '0')
 
byte b=9;
string res="";
for (int i=7;i>=0;i++)
res+=((b>>i) & 1).ToString();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top