11,576,667 members (61,194 online)
Rate this:
See more:
This is very annoying, a long time ago I needed to convert an integer to a binary value for uploading to a device. Worked it out good and gone. The sad thing is I now need to convert an integer to a binary value for something else. I really need to get this going

```           String s = "05";
int b = 0;

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(' ');
}
MessageBox.Show("'" + s + "' to binary: " + binary);

```
from that I want 5 to end up as 101, to get it to 00000101 I can append the zeros to the string.
If I run the code it locks up
Quote:
Serial_Number.Form1.button3_Click
(object sender = {Text = Cannot evaluate expression because the code of the current method is optimized.},
System.EventArgs e = {X = 46 Y = 6 Button = Left}) Line 77 + 0x40 bytes C#

and the debugger returns what am I doing wrong, I am starting to think I need a much deeper route through my system for the code.
Glenn
Posted 14-Jan-13 5:02am

Rate this:

## Solution 3

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();
// Initial length of the StringBuilder is 0
// The loop will be entered as the condition is less than or equal to 0
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(' ');
// StringBuilder Length has been increased by 9 characters
// so the loop continuation condition is still true
}```

I've always used System.Convert.ToString(Int32 value, Int32 base) to get 'binary' strings. It's very straightforward:
```// Assuming input is 8 bit
String s = Convert.ToString(5, 2);
// s = "101";
// result = "00000101"
```
glennPattonWork at 14-Jan-13 11:58am

Thanks for that the Padleft seems to get around the issue I was having with there not being 8 bits in my Binary!
Rate this:

## Solution 4

Please stop it. A number is already binary, unless it's really a number, not a string representation of a number.
This is the very basic understanding, you need to learn basics first.

Yes, I understand what did you want, some string data as "001010101…". This is a string! Don't mix is with binary, which is… everything, in fact.

—SA
v2
Rate this:

## Solution 5

How about (.NET 3.5, `using System.Linq;`):
```static string Bits(int v)
{
return BitConverter
.GetBytes(v)
.Reverse()
.Aggregate("", (s, b) => s + Convert.ToString(b, 2).PadLeft(8, '0'));
}
static string Bits(float v)
{
return BitConverter
.GetBytes(v)
.Reverse()
.Aggregate("", (s, b) => s + Convert.ToString(b, 2).PadLeft(8, '0'));
}
etc.
```

You may play with the Reverse() to get the byte order you want.
Cheers
Andi

 0 Sergey Alexandrovich Kryukov 251 1 Abhinav S 170 2 Lucifier Rocks 105 3 DamithSL 95 4 Dave Kreskowiak 80
 0 OriginalGriff 705 1 Sergey Alexandrovich Kryukov 626 2 Abhinav S 483 3 F-ES Sitecore 410 4 Suvendu Shekhar Giri 365

Web03 | 2.8.150603.1 | Last Updated 14 Jan 2013