[No000071] C # Binary conversion (binary, 16, decimal reciprocal)

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebinary Conversion {classProgram {Static voidMain (string[] args) {            //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"            intD =Ten; //decimal into binary stringConsole.WriteLine (Convert.ToString (D,2));//output: 1010//decimal to hexadecimal stringConsole.WriteLine (Convert.ToString (D, -));//output: A//Binary string to decimal number            stringBin ="1010"; Console.WriteLine (Convert.ToInt32 (Bin,2));//Output: Ten//Binary string to hexadecimal number            stringBin2 ="1010"; Console.WriteLine (string. Format ("{0:x}", Convert.ToInt32 (Bin2,2)));//output: A//Hex -to-binary stringConsole.WriteLine (Convert.ToString (0xa,2));//output: 1010//hexadecimal turn decimal numberConsole.WriteLine (Convert.ToString (0xa,Ten));//Output: Ten//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. //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" represents a-f using lowercase letters, while "X" means a-f is represented by large letters. The following example:            intA =188; Console.WriteLine ();//The results of the operation are as follows:Console.WriteLine ("A (Ten) ="+ a.tostring () +"\ n");//A (Ten) = 188Console.WriteLine ("A (+) ="+ a.tostring ("x") +"\ n");//A (+) = BCConsole.WriteLine ("A (+) ="+ a.tostring ("X") +"\ n");//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:Console.WriteLine ("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:            intb =int. Parse ("AB", System.Globalization.NumberStyles.HexNumber);//The value of the last obtained B is 171. Console.WriteLine (b); A=171; Console.WriteLine ("A (+) ="+ a.tostring ("X4") +"\ n");        Console.readkey (); }    }}

[No000071] C # Binary conversion (binary, 16, decimal reciprocal)

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.