I'm not sure what you intended the outer loop should do, but it appears to be infinite.
I've commented the code to explain why:
StringBuilder binary = new StringBuilder();
for (int j = 0; j <= binary.Length; j++ )
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.Append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.Append(' ');
}
I've always used System.Convert.ToString(Int32 value, Int32 base) to get 'binary' strings. It's very straightforward:
String s = Convert.ToString(5, 2);
String result = s.PadLeft(8, '0');