G Guest Dec 28, 2005 #1 Can anyone tell me how to convert a byte to bit pattern? e.g. Byte b = 1; after conversion = 00000001 Tedmond
Can anyone tell me how to convert a byte to bit pattern? e.g. Byte b = 1; after conversion = 00000001 Tedmond
J Jon Skeet [C# MVP] Dec 28, 2005 #2 Tedmond said: Can anyone tell me how to convert a byte to bit pattern? e.g. Byte b = 1; after conversion = 00000001 Click to expand... 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')
Tedmond said: Can anyone tell me how to convert a byte to bit pattern? e.g. Byte b = 1; after conversion = 00000001 Click to expand... 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')
G Guest Dec 28, 2005 #3 byte b=9; string res=""; for (int i=7;i>=0;i++) res+=((b>>i) & 1).ToString();