I am reading from serial port using string messaga = _serialPort.ReadLine();
When i Console.WriteLine(messaga); random characters appear on the screen which is logical because the binary data is non ASCII.
I suppose the method I am using handles data as ascii.
What i would like to do is create a string var and asign to it the binary raw data coming from the port so when I console.write this var I want to see a string with binary data like 1101101110001011010 and NOT characters. How can i manage that?
            Asked
            
        
        
            Active
            
        
            Viewed 2,018 times
        
    0
            
            
        
        jayt csharp
        
- 435
 - 3
 - 8
 - 12
 
- 
                    Do you have an example of the "characters" being displayed? – Gregory A Beamer Jun 08 '11 at 21:55
 - 
                    Did you really expect it to convert all the bits to a string of 10100010 etc? – BugFinder Jun 08 '11 at 21:58
 - 
                    Are we really here just for the reputation count? – Mike Miller Jun 08 '11 at 22:02
 - 
                    2Does `_serialPort` have a method for reading a `byte[]` vs a `String`? It seems like you would be better off using that method instead if you want to print the bits. – dana Jun 08 '11 at 22:09
 - 
                    1We are not here for reputation,but we like to know is our solution correct or not!,and what is best solution to amend ourself – DeveloperX Jun 08 '11 at 22:14
 - 
                    @Mike Miller - It's not the rep count itself, but there's a level of disrespect when someone helps you and there's no 'thank you' comment or acknowledgement (by upvote/marking as answer). From a practical POV, it makes it easier to scroll through posts and find out whether it contains an answer or not. – keyboardP Jun 08 '11 at 22:31
 - 
                    @keyboardP fair point and taken onboard. I took a bit of umbrage in the tone of the comments i.e. get your affairs in order and then we bid you worthy of help. – Mike Miller Jun 08 '11 at 22:34
 - 
                    1@Mike Miller - Nothing like that :)- It's understandable when people are new here since it's quite different from 'normal' forums in that respect. – keyboardP Jun 08 '11 at 22:44
 
2 Answers
5
            
            
        Stolen from How do you convert a string to ascii to binary in C#?
foreach (string letter in str.Select(c => Convert.ToString(c, 2)))
{
  Console.WriteLine(letter);
}
        Community
        
- 1
 - 1
 
        Mike Miller
        
- 16,195
 - 1
 - 20
 - 27
 
- 
                    1-1 for not getting it right. The results from `Convert.ToString(c,2)` aren't padded with leading zeros to the correct width for the type (e.g. conversion of `(byte)0x01` yields `"1"` rather than `"00000001"`). – Nicholas Carey Jun 08 '11 at 22:52
 
0
            You mean like this?
class Utility
{
  static readonly string[] BitPatterns ;
  static Utility()
  {
    BitPatterns = new string[256] ;
    for ( int i = 0 ; i < 256 ; ++i )
    {
      char[] chars = new char[8] ;
      for ( byte j = 0 , mask = 0x80 ; mask != 0x00 ; ++j , mask >>= 1 )
      {
        chars[j] = ( 0 == (i&mask) ? '0' : '1' ) ;
      }
      BitPatterns[i] = new string( chars ) ;
    }
    return ;
  }
  const int BITS_PER_BYTE = 8 ;
  public static string ToBinaryRepresentation( byte[] bytes )
  {
    StringBuilder sb = new StringBuilder( bytes.Length * BITS_PER_BYTE ) ;
    foreach ( byte b in bytes )
    {
      sb.Append( BitPatterns[b] ) ;
    }
    string instance = sb.ToString() ;
    return instance ;
  }
}
class Program
{
  static void Main()
  {
    byte[] foo = { 0x00 , 0x01 , 0x02 , 0x03 , } ;
    string s   = Utility.ToBinaryRepresentation( foo ) ;
    return ;
  }
}
Benchmarked this just now. The above code is approximately 12x faster than using Convert.ToString() and approximately 17x faster if the correction is added to pad with leading '0's.
        Nicholas Carey
        
- 71,308
 - 16
 - 93
 - 135