12,627,681 members (33,869 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 6: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";
String result = s.PadLeft(8, '0');
// result = "00000101"
```
glennPattonWork 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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 ppolymorphe 228 OriginalGriff 172 Peter Leow 156 CHill60 116 Richard MacCutchan 100
 OriginalGriff 1,334 ppolymorphe 964 John Simmons / outlaw programmer 600 Peter Leow 539 CPallini 523

Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 14 Jan 2013