# Realization of C binary conversion (binary hexadecimal decimal conversion)

• 2021-11-13 07:07:57
• OfStack

Because binary numbers cannot be represented directly in C #, all binary numbers are represented by a string
For example: binary: 1010 denoted as string: "1010"
int d = 10;

``````
//10 Binary conversion 2 Binary string
Console.WriteLine(Convert.ToString(d,2));
// Output : 1010

//10 Binary conversion 106 Binary string
Console.WriteLine(Convert.ToString(d,16));
// Output : a

//2 Binary string conversion 10 Binary number
string bin = "1010";
Console.WriteLine(Convert.ToInt32(bin,2));
// Output : 10

//2 Binary string conversion 106 Binary number
string bin = "1010";
Console.WriteLine(string.Format("{0:x}",Convert.ToInt32(bin,2));
// Output : a

//106 Binary conversion 2 Binary string
Console.WriteLine(Convert.ToString(0xa,2));
// Output : 1010

//106 Binary conversion 10 Binary number
Console.WriteLine(Convert.ToString(0xa,10));
// Output : 10

``````

C # implements conversion to 106-ary

C # language has a lot to learn, here we mainly introduce C # to achieve the conversion of 106 binary, including the introduction of the enumeration value used to represent 106 binary is HexNumber and so on.

Any data in the computer is stored in binary system, so binary system has nothing to do with data storage, only with input and output. Therefore, for binary conversion, we only care about the result in the string.

The ToString () method mentioned in article 4 above converts numeric values to strings, but in strings the results are displayed in decimal. Now, by adding a few parameters to it, we can convert the C # implementation to 106-ary-using the ToString (string) method.

Here, you need a parameter of type string, which is the format specifier. The format specifiers in the 106 system are "x" or "X". The difference between the two format specifiers lies mainly in the six digits of A-F: "x" stands for a-f, which is represented by lowercase letters, while "X" stands for A-F, which is represented by large letters. The following example:

``````
private void TestHex() {
int a = 188;
thistextBoxText = "";
thistextBoxAppendText("a(10) = " + aToString() + "\n");
thistextBoxAppendText("a(16) = " + aToString("x") + "\n");
thistextBoxAppendText("a(16) = " + aToString("X") + "\n");
} ``````

The running results are as follows:

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

At this time, we may have another requirement, that is, in order to display the results neatly, we need to control the length of the 106-ary representation, and if the length is not enough, fill it with the leading 0. To solve this problem, we only need to write the number indicating the length after the format specifier "x" or "X". For example, to limit the length to 4 characters, you can write "X4". Add a sentence to the above example:

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

``````

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

Now, let's talk about 1. How to convert a string representing a 106-ary number into an integer. This 1 transformation also needs the help of Parse () method. Here, I need the Parse (string, System. Globalization. NumberStyles) method. The first parameter is a string representing a 106-ary number, such as "AB", "20" (representing 32 in 10), and so on. The second parameter, System. Globalization. NumberStyles, is an enumeration type and is used to indicate that the enumeration value in the 106-ary system is HexNumber. Therefore, if we were to convert "AB" to an integer, we should write this: int b = int. Parse ("AB", System. Globalization. NumberStyles. HexNumber), and the resulting value of b is 171.

Related articles: