7

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

4
  • This post seems to have what you seek. It looks like whatever you have is converting it to hexadecimal. Commented May 20, 2013 at 23:20
  • This looks like a recent question that was deleted. Commented May 20, 2013 at 23:23
  • And what exactly does 'binary' mean here? Both the original string and the byte[] are binary, like everything in a computer. Commented May 20, 2013 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. Commented May 20, 2013 at 23:28

5 Answers 5

19

Here is an example:

foreach (char c in "Test") Console.WriteLine(Convert.ToString(c, 2).PadLeft(8, '0')); 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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'); } 

Comments

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()

Comments

-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 ; } 

4 Comments

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: 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.
Surely loop unrolling is not something you should be doing yourself. The jit would do that as a basic optimisation.
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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.