3

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

2 Answers 2

5

This should do it:

string binaryText = "1101000";
int value1 = Convert.ToInt32(binaryText, 2) // 104
byte value2 = Convert.ToByte(binaryText, 2); // 104
Sign up to request clarification or add additional context in comments.

Comments

1

to convert into byte array:

System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
byte [] dBytes = encoding.GetBytes(str);

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.