Because the binary number cannot be represented directly in C #, all binary numbers are represented by a string
For example: binary: 1010 is represented as a string: "1010"
int d = 10;
Decimal into binary string
Console.WriteLine (Convert.ToString (d,2));
Output: 1010
Decimal to hexadecimal string
Console.WriteLine (Convert.ToString (d,16));
Output: A
binary string to decimal number
String bin = "1010";
Console.WriteLine (Convert.ToInt32 (bin,2));
Output: 10
binary string to hexadecimal number
String bin = "1010";
Console.WriteLine (String. Format ("{0:x}", Convert.ToInt32 (bin,2));
Output: A
Hex-to-binary string
Console.WriteLine (Convert.ToString (0xa,2));
Output: 1010
Hexadecimal Turn decimal number
Console.WriteLine (Convert.ToString (0xa,10));
Output: 10
C # Implementation Convert hex
The C # language has a lot to learn, here we mainly introduce the C # implementation of the conversion hex, including the introduction of the hexadecimal enumeration value is hexnumber and so on.
C # Implementation Convert hex
Any data inside the computer is stored in binary, so the binary is not related to the storage of the data, only the input and output. So, for a binary conversion, we only care about the results in the string.
The ToString () method is mentioned in the 4th above to convert a numeric value to a string, but in a string, the result is displayed in decimal. Now let's add some arguments to it so that the C # implementation can be converted to hexadecimal-using the ToString (string) method.
Here you need a string type argument, which is the format specifier. The hexadecimal format specifier is "x" or "X", and the difference between the two format specifiers is mainly a-f six digits: "x" means a-f is denoted by lowercase letters, while "X" means a-f is denoted by large letters. The following example:
1. private void Testhex () {
2. int a = 188;
3. This.textBox1.Text = "";
4. This.textBox1.AppendText ("a (Ten) =" + a.tostring () + "\ n");
5. This.textBox1.AppendText ("a (+) =" + a.tostring ("x") + "\ n");
6. This.textBox1.AppendText ("a (+) =" + a.tostring ("X") + "\ n");
7.}
The results of the operation are as follows:
1. A (10) = 188
2. A (+) = BC
3. A (+) = BC
At this time, we may have another requirement, that is, in order to show the results neatly, we need to control the length of the hexadecimal representation, if the length is not enough, with a leading 0 fill. To solve this problem, we just need to write the number on the length after the format specifier "x" or "X". For example, to limit the length of 4 characters, you can write "X4". In the example above, add a sentence:
1. This.textBox1.AppendText ("a (+) =" + a.tostring ("X4") + "\ n");
The result will be output a (+) = 00BC.
Now, let's talk about how to convert a string that represents a hexadecimal number to an integer type. This conversion also requires the use of the Parse () method. Here, I need the Parse (string, System.Globalization.NumberStyles) method. The first parameter is a string that represents a hexadecimal number, such as "AB", "20" (representing the decimal 32), and so on. The second parameter, System.Globalization.NumberStyles, is an enumeration type that is used to denote that the hexadecimal enumeration value is hexnumber. So, if we're going to convert "AB" to an integer, we should write this: int b = Int. Parse ("AB", System.Globalization.NumberStyles.HexNumber), the last obtained value of B is 171.
C # Binary conversion (binary, 16, decimal reciprocal)