Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
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




C#
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

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:
C#
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:
C#
// Assuming input is 8 bit
String s = Convert.ToString(5, 2);
// s = "101";
String result = s.PadLeft(8, '0');
// result = "00000101"
 
Share this answer
 
Comments
glennPattonWork3 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!
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
 
Share this answer
 
v2
How about (.NET 3.5, using System.Linq;):
C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900