" Cinta seperti bunga mawar yang indah nan rupawan tetapi juga berduri, barang siapa yang tertusuk durinya maka tak ada satu obat atau seorang tabib pun yang mampu "mengobatinya" sehingga luka itu sembuh dengan sendirinya jika waktunya tiba "  —  Syaikh Nazimi - Layla Majnun
 

Binary String Conversion to Decimal and Hex Using C#

more...

microsoftnet-logoIf you need to convert Binary String to Decimal (Integer) and Hexadecimal, .NET Framework has conversion methods in Convert class to make numeric conversion easier. This class is located in Microsoft.VisualBasic library.

To use Convert class in Microsoft.VisualBasic library, you need to include the following code at the top of your code:

using Microsoft.VisualBasic;

Converting binary String value to Int32 use Convert.ToInt32(String value, Int fromBase) method. value is the binary String value to convert, and formBase is 2 to indicate that value is binary.

String value = "00000010";
Int32 decimal = Convert.ToInt32(value, 2);

Converting decimal (Int32) value to hexadecimal is as easy as converting binary String to Int32 by using Convert.ToString(Int32 value, toBase) method. value is the decimal value to convert to hexadecimal, and toBase is 16 to indicate that value is converted to 16-base (hex).

Int32 value = 13;
String hex = Convert.ToString(value, 16);

You can combine both method to do binary to hex conversion like the following example:

String binary = "00000010";
String hex = Convert.ToString(Convert.ToInt32(binary, 2), 16);
0
Share up your minds and leave a comment
No one has commented yet. Be the first to comment!
Comment Form
XHTML Expert!
You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

* = required fields