Convert between decimal, hexadecimal, octal, and binary in C#

Converting between these bases is easy if you use the Convert class's ToInt64 and ToString methods. For example, the following statement parses the text in the TextBox named source and saves the result in the long variable value. The "16" means the Toint64 method should parse the text as a base 16 (hexadecimal) value.

value = Convert.ToInt64(source.Text, 16);

The following code does the opposite: it converts the value in variable value into a hexadecimal string and displays it in the TextBox named txtHexadecimal.

txtHexadecimal.Text = Convert.ToString(value, 16)

To parse and display values in other bases, simply replace 16 with the base: 8 for octal and 2 for binary.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.