6

I want to convert string to binary and I tried this code

byte[] arr = System.Text.Encoding.ASCII.GetBytes(aa[i]);

and this

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] arr= encoding.GetBytes(aa[i]);

but it returned numbers not binary say If I write 'm' it convert it to "0109" and I want to convert to zeros & Ones only Thanks in advance

Sam
  • 4,475
  • 2
  • 28
  • 31
Kyara Programmer
  • 93
  • 1
  • 2
  • 4
  • This [post](http://stackoverflow.com/a/917190/1758149) seems to have what you seek. It looks like whatever you have is converting it to hexadecimal. – RyPope May 20 '13 at 23:20
  • This looks like a recent question that was deleted. – H H May 20 '13 at 23:23
  • And what exactly does 'binary' mean here? Both the original string and the `byte[]` are binary, like everything in a computer. – H H May 20 '13 at 23:25
  • @RyPope I think it's just converting to 1 byte integers, not hex. So `A` becomes 65 (it's 1 byte ASCII representation) which is equal to 01000001 in binary. – evanmcdonnal May 20 '13 at 23:28

5 Answers5

18

Here is an example:

foreach (char c in "Test")
    Console.WriteLine(Convert.ToString(c, 2).PadLeft(8, '0'));
mitja.gti
  • 251
  • 1
  • 9
3

I'm not sure I completely understand your question, but if you want to convert text to binary this is how it is done:

public byte[] ToBinary(string stringPassed)
{
   System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
   return encoding.GetBytes(stringPassed);
}

You need to pass the whole string, not the characters in the string.

LemonCool
  • 1,240
  • 10
  • 18
2

So you can get to bytes, now you want to output 0s and 1s.

For a single byte b

var sb = new StringBuilder();
for(var i=7;i>=0;i--)
{
  sb.Append((b & (1<<i))==0?'0':'1');
}
weston
  • 54,145
  • 21
  • 145
  • 203
1

You could use : Convert.ToByte(string value, int fromBase)

According to MSDN :

fromBase Type: System.Int32 The base of the number in value, which must be 2, 8, 10, or 16.

For more detail, see this link : Convert.ToByte()

cat916
  • 1,363
  • 10
  • 18
-1

Something like this ought to do you;

static string bytes2bin( byte[] bytes )
{
  StringBuilder buffer = new StringBuilder(bytes.Length*8) ;
  foreach ( byte b in bytes )
  {
    buffer.Append(lookup[b]) ;
  }
  string binary = buffer.ToString() ;
  return binary ;
}

static readonly string[] lookup = InitLookup() ;
private static string[] InitLookup()
{
  string[] instance = new string[1+byte.MaxValue] ;
  StringBuilder buffer = new StringBuilder("00000000") ;
  for ( int i = 0 ; i < instance.Length ; ++i )
  {

    buffer[0] = (char)( '0' + ((i>>7)&1) ) ;
    buffer[1] = (char)( '0' + ((i>>6)&1) ) ;
    buffer[2] = (char)( '0' + ((i>>5)&1) ) ;
    buffer[3] = (char)( '0' + ((i>>4)&1) ) ;
    buffer[4] = (char)( '0' + ((i>>3)&1) ) ;
    buffer[5] = (char)( '0' + ((i>>2)&1) ) ;
    buffer[6] = (char)( '0' + ((i>>1)&1) ) ;
    buffer[7] = (char)( '0' + ((i>>0)&1) ) ;

    instance[i] = buffer.ToString() ;
  }
  return instance ;
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • I like the idea of caching the lookup, but I don't think I can upvote an answer with 8 copied and pasted lines. Why no `for` loop here? – weston May 21 '13 at 08:13
  • @weston: eh? There's no inner for-loop in the initializer because it's [theoretically] a little faster to unroll the loop at the cost of a few extra instructions. Not that efficiency for either CPU usage or memory matters one bit for this. More importantly, unrolling the inner loop makes the code much easier to understand. If I actually felt the need to do something like this for a production program, I'd gen the array declaration with the 256 string representations with perl and that into my code. String representations of octets in binary are unlikely to change anytime in the near future. – Nicholas Carey May 21 '13 at 17:18
  • Surely loop unrolling is not something you should be doing yourself. The jit would do that as a basic optimisation. – weston May 21 '13 at 17:45
  • Did you read my comment above? "**More importantly, unrolling the inner loop makes the code much easier to understand.**" Clarity of meaning is particularly important on public *fora* like SO, where the comprehension level of the audience is unknown (and likely to be lower than one might like). – Nicholas Carey May 21 '13 at 18:13