Realization of c-ary conversion (binary, hexadecimal, decimal mutual conversion)


Since binary numbers cannot be represented directly in C, all binary numbers are represented by a string
For example: binary: 1010 means string: “1010”
int d = 10;

//Decimal to binary string
//Output: 1010

//Decimal to hexadecimal string
// output: a

//Binary string to decimal
string bin = "1010";
// output: 10

//Binary string to hexadecimal number
string bin = "1010";
// output: a

//Hexadecimal to binary string
//Output: 1010

//Hexadecimal to decimal number
// output: 10

C ා conversion hex

C ා language has a lot to learn. Here we mainly introduce the implementation of C ා to convert hexadecimal, including the enumeration value used to represent hexadecimal is hexnumber and so on.

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

As mentioned in Item 4 above, toString () method can convert numeric value into string, but in string, the result is displayed in decimal system. Now let’s add some parameters to it, and we can convert the C ා implementation to hexadecimal – using the toString (string) method.

Here you need a parameter of type string, which is the format specifier. The hexadecimal format specifier is “X” or “X”. The difference between the two format specifiers lies in the six numbers of A-F: “X” represents A-F in lowercase letters, while “X” represents A-F in 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 operation 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 hexadecimal representation. If the length is not enough, fill it with leading 0. To solve this problem, we just need to write the length number after the format specifier “X” or “X”. For example, to limit the length to four characters, 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 how to convert a string representing a hexadecimal number to an integer. This transformation also needs to rely on the parse () method. Here, I need the parse (string, system. Globalization. Numberstyles) method. The first parameter is a string representing hexadecimal numbers, such as “ab”, “20” (representing 32 decimal), etc. 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 integer, we should write as follows: int b = int.parse (“ab”, system. Globalization. Numberstyles. Hexnumber), and the final value of B is 171.    

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.