C # hexadecimal conversion (binary, hexadecimal, and decimal conversion)

Source: Internet
Author: User
Tags decimal to binary

C # hexadecimal conversion (binary, hexadecimal, and decimal conversion)
Since the binary number cannot be directly expressed in C #, all binary numbers are represented by one string.
For example, binary: 1010 represents a string: "1010"
Int d = 10;


// Convert decimal to binary string
Console. WriteLine (Convert. ToString (d, 2 ));
// Output: 1010

 


// Convert decimal to hexadecimal string
Console. WriteLine (Convert. ToString (d, 16 ));
// Output:


// Convert the binary string to the decimal number
String bin = "1010 ";
Console. WriteLine (Convert. ToInt32 (bin, 2 ));
// Output: 10


// Convert a binary string to a hexadecimal number
String bin = "1010 ";
Console. WriteLine (string. Format ("{0: x}", Convert. ToInt32 (bin, 2 ));
// Output:


// Convert hexadecimal to binary string
Console. WriteLine (Convert. ToString (0xa, 2 ));
// Output: 1010


// Hexadecimal to decimal number
Console. WriteLine (Convert. ToString (0xa, 10 ));
// Output: 10


C # implement hexadecimal conversion
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:


1. private void TestHex (){
2. int a = 188;
3. this. textBox1.Text = "";
4. this. textBox1.AppendText ("a (10) =" + a. ToString () + "\ n ");
5. this. textBox1.AppendText ("a (16) =" + a. ToString ("x") + "\ n ");
6. this. textBox1.AppendText ("a (16) =" + a. ToString ("X") + "\ n ");
7 .}


The running result is as follows:


1. a (10) = 188
2. a (16) = bc
3. 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:


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


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.
 

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.