Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from string bits to byte or hex

Tags:

c#

.net

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

like image 649
Hadad Avatar asked Feb 04 '26 13:02

Hadad


2 Answers

This should do it:

string binaryText = "1101000";
int value1 = Convert.ToInt32(binaryText, 2) // 104
byte value2 = Convert.ToByte(binaryText, 2); // 104
like image 165
Jon Skeet Avatar answered Feb 07 '26 02:02

Jon Skeet


to convert into byte array:

System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
byte [] dBytes = encoding.GetBytes(str);
like image 42
Wael Dalloul Avatar answered Feb 07 '26 04:02

Wael Dalloul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!