tags:

views:

91

answers:

2

I have a string that contains the following 7 bits: 1101000. How can I convert it to a byte or int?

+5  A: 

This should do it:

string binaryText = "1101000";
int value1 = Convert.ToInt32(binaryText, 2) // 104
byte value2 = Convert.ToByte(binaryText, 2); // 104
Jon Skeet
+1, You are right, I misunderstood the question.
Wael Dalloul
+1  A: 

to convert into byte array:

System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
byte [] dBytes = encoding.GetBytes(str);
Wael Dalloul
Well that will convert it into a byte *array*, but almost certainly not in the way that the OP wants. (There's no need to create a new instance of `ASCIIEncoding`, btw - just use `Encoding.ASCII`.)
Jon Skeet