Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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: bad
good
Please Sign up or sign in to vote.

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"
  Permalink  
Comments
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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 392
1 OriginalGriff 370
2 CPallini 190
3 Abdul Samad KP 145
4 George Jonsson 119
0 OriginalGriff 6,329
1 Sergey Alexandrovich Kryukov 5,700
2 CPallini 4,940
3 George Jonsson 3,469
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100