Example Analysis of Convert Class solution in C #
This article mainly introduces the example analysis of Convert class solutions in C#, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
You have a string that contains binary, octal, decimal, or hexadecimal numbers. You need to convert it to an equivalent value and represent it in decimal system.
C # Convert class workaround:
To convert a number from another base to a decimal number, simply use the overloaded static method Convert.ToInt32 of the C # Convert class:
String base2 = "11"; string base8 = "17"; string base10 = "110"; string base16 =" 11FF "; Console.WriteLine (" Convert.ToInt32 (base2, 2) = "+ Convert.ToInt32 (base2, 2)); Console.WriteLine (" Convert.ToInt32 (base8, 8) = "+ Convert.ToInt32 (base8, 8)); Console.WriteLine (" Convert.ToInt32 (base10, 10) = "+ Convert.ToInt32 (base10, 10)) Console.WriteLine ("Convert.ToInt32 (base16, 16) =" + Convert.ToInt32 (base16, 16))
The code produces this output:
Convert.ToInt32 (base2, 2) = 3 Convert.ToInt32 (base8, 8) = 15 Convert.ToInt32 (base10, 10) = 110 Convert.ToInt32 (base16, 16) = 4607
Discussion:
The static method Convert.ToInt32 takes two parameters, a string containing a number and an integer representing the numeric base. This function then converts the number string to a decimal integer.
Other static functions of the C # Convert class, such as ToByte,ToInt64 and ToInt16, have similar overloads, which accept a number of string types and an integer that represents the numeric base. However, these methods can only convert binary, octal, decimal, or hexadecimal numbers. They can only convert these numbers to decimal numbers. However, the ToString of any type of numeric value can be converted to string.
Thank you for reading this article carefully. I hope the article "sample Analysis of Convert Solutions in C#" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!