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
2009-09-29 09:41:34
+1, You are right, I misunderstood the question.
Wael Dalloul
2009-09-29 09:49:39
+1
A:
to convert into byte array:
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
byte [] dBytes = encoding.GetBytes(str);
Wael Dalloul
2009-09-29 09:44:30
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
2009-09-29 09:45:35