Convert.ToInt32()方法 (Convert.ToInt32() Method)
Convert.ToInt32() is a predefined method in C#, which returns an integer value (in 32 bits) from given various types of values.
Convert.ToInt32()是C#中的预定义方法,它从给定的各种类型的值中返回一个整数值(32位)。
Here, we will go with some of the conversion…
在这里,我们将进行一些转换…
Syntax:
句法:
Convert.ToInt32(input, base);
Here,
这里,
-
input is the input string that may contain variable format’s value like decimal/number value, octal value or hexadecimal value.
输入是可能包含可变格式的值(例如十进制/数字值,八进制值或十六进制值)的输入字符串。
-
base is the number system base like, 10 for decimal (which we do not need to write while calling the function), 8 for octal and 16 for the hexadecimal value.
base是一个数字系统的基数,例如10代表十进制 (调用函数时不需要写), 8代表八进制 , 16代表十六进制值 。
Code:
码:
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string input = "";
int num = 0;
try
{
input = "12345"; //value is a decimal formatted number
num = Convert.ToInt32(input); //base is an optional if string contains decimal value
Console.WriteLine("num (decimal string to integer) :" + num);
//we can also provide the base of the input - it is decimal value
//so, 10 can be used as base
num = Convert.ToInt32(input, 10);
Console.WriteLine("num (decimal string to integer) :" + num);
//convert octal string to integer
input = "30071";
num = Convert.ToInt32(input, 8);
Console.WriteLine("num (octal string to integer) :" + num);
//convert hex string to integer
input = "3039ACFE";
num = Convert.ToInt32(input, 16);
Console.WriteLine("num (hex string to integer) :" + num);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
num (decimal string to integer) :12345
num (decimal string to integer) :12345
num (octal string to integer) :12345
num (hex string to integer) :809086206
今天的文章使用C#中的Convert.ToInt32()将十进制,八进制,十六进制字符串转换为整数分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/24421.html