How can I get a bit from a string in C #?

If I had the next line "Blue Box", how could I get the bits that make up the line in C # and what data type I would save.

If I make the letter "o", I get 111 as bytes and 111 as bits. This is chopping 0, and if I do “oo”, I get 111 for every o in the byte array, but for bits I get the value 28527. Why?

+6
c #
source share
3 answers

If you need bits in a string format, you can use this function:

public string GetBits(string input) { StringBuilder sb = new StringBuilder(); foreach (byte b in Encoding.Unicode.GetBytes(input)) { sb.Append(Convert.ToString(b, 2)); } return sb.ToString(); } 

If you use the Blue Box example, you get:

 string bitString = GetBits("Blue Box"); // bitString == "100001001101100011101010110010101000000100001001101111011110000" 
+14
source share

You can do the following:

 byte[] bytes = System.Text.UTF8Encoding.Default.GetBytes("Blue Box"); BitArray bits = new System.Collections.BitArray(bytes); 
+14
source share

It depends on what you mean by "bits." Are you talking about an ASCII view? Utf8? UTF16? The System.Text.Encoding namespace should get started.

+5
source share

All Articles