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

Source: Internet
Author: User
Tags binary to decimal decimal to binary

C # its built-in X-in conversion function is as follows:

// Convert decimal to binary
Console. WriteLine (Convert. ToString (69, 2 ));
// Convert decimal to octal
Console. WriteLine (Convert. ToString (69, 8 ));
// Convert decimal to hexadecimal
Console. WriteLine (Convert. ToString (69, 16 ));

// Convert binary to decimal
Console. WriteLine (Convert. ToInt32 ("100111101", 2 ));
// Octal to decimal
Console. WriteLine (Convert. ToInt32 ("76", 8 ));
// Convert hexadecimal to decimal
Console. WriteLine (Convert. ToInt32 ("FF", 16 ));

Hexadecimal to decimal:

 

 

Convert decimal to hexadecimal:

This. myResult. Text = myTempR. ToString ("X ");

C # language is worth learning a lot. Here we mainly introduce how C # converts hexadecimal notation, including the HexNumber enumeration value used to represent hexadecimal notation.

C # implement hexadecimal conversion

Any data is saved in binary in the computer. Therefore, the binary system is irrelevant to the data storage and only related to the input and output. Therefore, for hexadecimal conversion, We only care about the results in the string.

The ToString () method can be used to convert a value into a string, but in the above 4th, the result is displayed in decimal. Now we can add some parameters to it so that C # can be converted to hexadecimal -- use the ToString (string) method.

Here we need a string-type parameter, which is the format specifier. The hexadecimal format specifier is "x" or "X". The difference between the two format specifiers lies in the A-F of six numbers: "x" indicates that a-f is represented by lowercase letters, while "X" indicates that the A-F is represented by uppercase letters. For example:

private void TestHex() {  
int a = 188;  
this.textBox1.Text = "";  
this.textBox1.AppendText("a(10) = " + a.ToString() + "");  
this.textBox1.AppendText("a(16) = " + a.ToString("x") + "");  
this.textBox1.AppendText("a(16) = " + a.ToString("X") + "");  
} 

The running result is as follows:

a(10) = 188  
a(16) = bc  
a(16) = BC 

At this time, we may have another requirement, that is, to display the result neatly, we need to control the length of the hexadecimal representation. If the length is not enough, fill it with the leading 0. To solve this problem, we only need to write a number indicating the length after the format specifier "x" or "X. For example, to limit the length to 4 characters, you can write it as "X4 ". In the preceding example, add the following sentence:

this.textBox1.AppendText("a(16) = " + a.ToString("X4") + ""); 

The output result is a (16) = 00BC.

Now let's talk about how to convert a string that represents a hexadecimal number into an integer. This conversion also requires the Parse () method. Here, I need the Parse (string, System. Globalization. NumberStyles) method. The first parameter is a hexadecimal string, such as "AB" and "20" (32 in decimal format. The second parameter System. Globalization. NumberStyles is an enumeration type, which indicates that the hexadecimal enumeration value is HexNumber. Therefore, if we want to convert "AB" to an integer, we should write: int B = int. parse ("AB", System. globalization. numberStyles. hexNumber), and the final value of B is 171.

 

Int myTempR = int. Parse ("EA", System. Globalization. NumberStyles. HexNumber );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.